Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > GUI - GUI value passing

Reply
Thread Tools

GUI - GUI value passing

 
 
paul.foreman
Guest
Posts: n/a
 
      10-22-2004
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


 
Reply With Quote
 
 
 
 
Michael Rauscher
Guest
Posts: n/a
 
      10-22-2004
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
 
Reply With Quote
 
 
 
 
Jacob
Guest
Posts: n/a
 
      10-23-2004
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.

 
Reply With Quote
 
paul.foreman
Guest
Posts: n/a
 
      10-24-2004

"Jacob" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      10-25-2004
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.

 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      10-25-2004
Hi Paul,

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


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();
}
}

>
> Regards
>
> Paul


Bye
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Baseline GUI Architecture (was: Baseline GUI Prototype?) Stefan Ram Java 3 11-20-2011 02:18 AM
PyGTK GUI update without signals from GUI Andrew Lapidas Python 0 04-12-2008 11:07 PM
Passing by const & and returning a temp vs passing by value and returningit Victor Bazarov C++ 25 03-23-2005 04:24 PM
GUI and non-GUI data Hal Fulton Ruby 1 08-05-2004 08:42 PM
[PY GUI] interest function in python GUI(wxpython,pyqt) program.wxpython,pyqt ulysses Python 4 10-22-2003 03:28 PM



Advertisments