Communicating between components
Hey folks,
Is there a "best practices" pattern to communicating between GUI Components?
For example: Say I have the following GUI component hierarchy:
Main Application:
JFrame (Main Application Window)
JPanel (Contains a textfield for displaying user input)
JTextField
JButton (When clicked, opens up a wizard that prompts the user for a string)
Wizard:
JDialog (Prompts the user for his/her name), on submit sends it back to the TextField
I hate asking this question, because it seems pretty fundamental, but I have one fairly large book on Swing, and have Googled fairly extensively, but is there another method of passing information between components besides creating a long chain of mutators?
If you know of any good resources on the subject, they would be much appreciated.
Thanks!
Joe
[879 byte] By [
Joe_ha] at [2007-11-27 7:37:53]

# 1
hi!
this can be achieved in many ways. depending upon the way you are now doing the job. so you need to create a
[url=http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example
Program[/url] (SSCCE) that demonstrates the incorrect behaviour.
Don't forget to use the [url=http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url]
so the posted code retains its original formatting.
regards
Aniruddha
# 3
don't know about 'best practice', but all you'd need in the button's actionPerformed() is
String textfieldText = new MyDialog().getData();
//check for null/empty/whatever
textField.setText(textfieldText);
where MyDialog is a class that extends JDialog, is modal, throw in a parent, and
the method getData() returns what is entered into the dialog field/s when the
dialog closes
# 4
... onButtonClicked()
{
MyDialog d = new MyDialog(this.textFieldValiable);
d.setVisible(true);
}
The idea is "only dialog knows when it submitted or canceled". So, it have to set fields text or not to set.
class MyDialog extends JDialog
{
private JTextField target;
public MyDialog(JTextField target)
{
this.target = target;
}
.... onSubmit()
{
target.setText("submitted");
}
}
Something like this....
Message was edited by:
FirstName2