GUI - GUI value passing

Discussion in 'Java' started by paul.foreman, Oct 22, 2004.

  1. paul.foreman

    paul.foreman Guest

    Hi,
    A quick newbie question.

    From a window created by extending Frame, I have created a new child window
    also based on extending Frame.

    I need the user to select a value in the child window which then gets passed
    back to the parent window. The child window then is closed.

    Any pointers would be welcome on how to get the user selected value back to
    the parent window.

    Regards

    Paul
     
    paul.foreman, Oct 22, 2004
    #1
    1. Advertising

  2. Hi Paul,

    paul.foreman wrote:
    > Hi,
    > A quick newbie question.
    >
    > From a window created by extending Frame, I have created a new child window
    > also based on extending Frame.
    >
    > I need the user to select a value in the child window which then gets passed
    > back to the parent window. The child window then is closed.
    >
    > Any pointers would be welcome on how to get the user selected value back to
    > the parent window.


    you could use a listener.

    public interface ValueListener {
    public void valueChanged( Object newValue );
    }

    class ParentFrame extends Frame implements ValueListener {

    // ...

    protected void createChildFrame() {
    ChildFrame frm = new ChildFrame();
    frm.addValueListener( this );
    frm.pack();
    frm.show();
    }

    public void valueChanged( Object newValue ) {
    // do s.th. with the given value
    }
    }

    class ChildFrame extends Frame {
    ArrayList listeners = new ArrayList();

    // ...

    public void addValueListener( ValueListener listener ) {
    listeners.add(listener);
    }

    protected void fireValueChanged( Object newValue ) {
    Iterator it = listeners.iterator();
    while ( it.hasNext() )
    ((ValueListener)it.next()).valueChanged(newValue);
    }
    }


    Now, react to the selection with a call to fireValueChanged. In more
    complex situations it's worth using MVC.

    Bye
    Michael
     
    Michael Rauscher, Oct 22, 2004
    #2
    1. Advertising

  3. paul.foreman

    Jacob Guest

    paul.foreman wrote:

    > From a window created by extending Frame, I have created a new child window
    > also based on extending Frame.
    >
    > I need the user to select a value in the child window which then gets passed
    > back to the parent window. The child window then is closed.


    If your parent window *conceptually* owns the child window, simply
    pass the parent window as parameter to the constructor of the child
    window and keep it as a class variable in the child. Make the
    appropriate value setter method in the parent and call that from your
    child window.

    If the two are *conceptually* independent, use an event mechanism as
    suggested by Michael R.
     
    Jacob, Oct 23, 2004
    #3
  4. paul.foreman

    paul.foreman Guest

    "Jacob" <> wrote in message
    news:...
    > paul.foreman wrote:
    >
    >> From a window created by extending Frame, I have created a new child
    >> window also based on extending Frame.

    >
    > If the two are *conceptually* independent, use an event mechanism as
    > suggested by Michael R.
    >

    I have tried to set up an event mechanism, with no luck so far.
    //code to add an action listener and create an ActionEvent.
    button2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    button2_actionPerformed(e) ;
    }
    });

    //code in the same class to close the window.
    void button2_actionPerformed(ActionEvent e) {
    dispose();
    }
    where sP is the reference to the instance of the class (window) which is
    generating the button event.

    //I have tried the following code in the other window, where sP is the
    reference to the instance of the class (window) which is generating the
    button event., but it does not compile

    void sP.button2_actionPerformed(ActionEvent e) {
    cPid = sP.cPids;
    }

    Any assistance welcome in trying to get the event mechanism working across
    different class instances.

    Regards

    Paul
     
    paul.foreman, Oct 24, 2004
    #4
  5. paul.foreman

    Jacob Guest

    paul.foreman wrote:

    > I have tried to set up an event mechanism, with no luck so far.


    It seems like you try to reuse the event mechanism for JButton,
    which is probably not a good idea. You need to decide your own
    events and implement a similar mechanism to support those.

    Look at the event manager class at http://geosoft.no/software/

    In your case (having one window W1 responding to a change in
    another window W2 window where the two are unawere of each other):

    o Decide on a event name: "<Something>Changed" for instance.
    o W1 should implement the EventListener interface and its
    update() method. Add it as listener in the constructor.
    o W2 should call EventManager.notify ("<Something>Changed").
    o Decide on what data that should be passed with the event so
    that W1 is able to update appropriately.
     
    Jacob, Oct 25, 2004
    #5
  6. Hi Paul,

    paul.foreman wrote:
    >
    > I have tried to set up an event mechanism, with no luck so far.


    Code:
    [color=blue]
    > Any assistance welcome in trying to get the event mechanism working across
    > different class instances.[/color]
    
    perhaps I should show a whole but stupid example:
    
    // File: ValueTest.java
    
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Iterator;
    import java.util.ArrayList;
    
    interface ValueListener {
    public void valueChanged( Object newValue );
    }
    
    class ChooserFrame extends Frame {
    private ArrayList valueListeners = new ArrayList();
    
    private List listBox = null;
    
    public ChooserFrame( String values[] ) {
    super("Value Editor");
    
    initListBox( values );
    setLayout( new BorderLayout() );
    
    Button button = new Button("OK");
    button.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    buttonActionPerformed();
    }
    } );
    
    addWindowListener( new WindowAdapter() {
    public void windowClosing( WindowEvent e ) {
    dispose();
    }
    } );
    
    add( listBox, BorderLayout.CENTER );
    add( button, BorderLayout.SOUTH );
    }
    
    private void initListBox( String values[] ) {
    listBox = new List(4, false);
    for ( int i = 0; i < values.length; i++ )
    listBox.add( values[i] );
    }
    
    private void buttonActionPerformed() {
    fireValueChanged( listBox.getSelectedItem() );
    dispose();
    }
    
    // listener stuff
    
    protected void fireValueChanged( Object newValue ) {
    Iterator it = valueListeners.iterator();
    while ( it.hasNext() )
    ((ValueListener)it.next()).valueChanged( newValue );
    }
    
    public void addValueListener( ValueListener listener ) {
    valueListeners.add( listener );
    }
    
    public void removeValueListener( ValueListener listener ) {
    valueListeners.remove( listener );
    }
    }
    
    class DisplayFrame extends Frame {
    private TextField textField;
    private static final String possibleValues[] = {
    "New York", "London", "Paris", "Berlin", "Milano", "Moscow"
    };
    
    public DisplayFrame() {
    super("Value Display");
    textField = new TextField( "", 10 );
    Button button = new Button("Choose");
    button.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    buttonActionPerformed();
    }
    });
    
    addWindowListener( new WindowAdapter() {
    public void windowClosing( WindowEvent e ) {
    dispose();
    }
    } );
    
    setLayout( new FlowLayout() );
    add( textField );
    add( button );
    }
    
    public void buttonActionPerformed() {
    ChooserFrame frm = new ChooserFrame( possibleValues );
    frm.addValueListener( new ValueListener() {
    public void valueChanged( Object newValue ) {
    if ( newValue != null )
    textField.setText( newValue.toString() );
    else
    textField.setText( "" );
    }
    } );
    
    frm.pack();
    frm.show();
    }
    }
    
    public class ValueTest {
    public static final void main( String args[] ) {
    DisplayFrame frame = new DisplayFrame();
    
    frame.pack();
    frame.show();
    }
    }
    
    Because DisplayFrame and ChooserFrame are conceptually independent, you
    can use the ChooserFrame everywhere you want to choose a value, e.g.
    
    public class ValueTest {
    public static final void main( String args[] ) {
    ChooserFrame frame =
    new ChooserFrame( new String[]{"1","2","3"} );
    
    frame.addValueListener( new ValueListener() {
    public void valueChanged( Object newValue ) {
    System.out.println( "Choosed: " + newValue );
    }
    });
    
    frame.pack();
    frame.show();
    }
    }
    [color=blue]
    >
    > Regards
    >
    > Paul[/color]
    
    Bye
    Michael
     
    Michael Rauscher, Oct 25, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Danielsson

    "Message passing"?, and a GUI question.

    Jan Danielsson, Jan 3, 2005, in forum: Java
    Replies:
    4
    Views:
    560
    Andrew Thompson
    Jan 4, 2005
  2. Victor Bazarov
    Replies:
    25
    Views:
    893
    E. Robert Tisdale
    Mar 23, 2005
  3. pereges
    Replies:
    7
    Views:
    438
    pereges
    Jun 1, 2008
  4. Ramon F Herrera
    Replies:
    8
    Views:
    483
    Pascal J. Bourguignon
    Sep 13, 2009
  5. deadpickle
    Replies:
    1
    Views:
    991
    Jens Thoms Toerring
    Nov 7, 2010
Loading...

Share This Page