GUI - GUI value passing

P

paul.foreman

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
 
M

Michael Rauscher

Hi Paul,

paul.foreman said:
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
 
J

Jacob

paul.foreman said:
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.
 
P

paul.foreman

Jacob said:
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
 
J

Jacob

paul.foreman said:
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.
 
M

Michael Rauscher

Hi Paul,

paul.foreman said:
I have tried to set up an event mechanism, with no luck so far.
Code:
Any assistance welcome in trying to get the event mechanism working across
different class instances.[/QUOTE]

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();
}
}
[QUOTE]
Regards

Paul[/QUOTE]

Bye
Michael
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top