aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Design consideration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Design consideration" Watch "Design consideration" New topic
Author

Design consideration

Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
Hi guys,

im currently creating an application(Alarm Clock) which can be comprise of several frame interacting with each other. I will have one main frame with a menu calling the other frames. But im also considering the use of JPanel and just past it on the contentpane of the main frame.

Another thing is how would i maintain the communication between frames. Here is an example, lets say Frame1 was displayed and has a button to display a second frame. The second frame has a textfield on it.

If i will close frame2 i want that text on its textfield to be displayed on frame1.

thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
make frame2 a modal dialog
then, in your actionListener that displays the dialog
String text = new MyDialog().getTextFieldValue();
//check text for null
frame1.setText(text)

the method getTextFieldValue() won't return until the dialog closes (if modal = yes)
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
@michael

are you referring to a dialog object? or a Frame object.. thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a dialog object

new MyDialog() would be your current frame2
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
@michael

what if i wanted to use a Frame object instead of a dialog. i still wanted the user to input some information. the new window will display several textfields.. How can my first frame communicate or get the values of the previous frame.

thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you just need to pass references between the objects.

here's a simple demo of one way.

click the button, enter some text into the textfield, click OK

Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
@mike

thanks. really a big help.


[ November 06, 2006: Message edited by: Cyrus Serrano ]
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
@mike,

you said this is just one solution, can you tell me another way to do this.

thanks again
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
because Frame2 is a JFrame, it needs a reference to Frame1 to pass the data to.
the reference in the example is the Frame1 instance.
the reference could be just the label.
in an actual program, it would generally be the Frame1 that is passed, and the
object (label) in Frame1 changed via a setter method (the label being private)

here's the same code, but using Frame2 as a JDialog.

no need to pass a reference, the method getData() does not return until the dialog closes,
due to setModal(true). This is not the same when Frame2 is a JFrame

Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
many thanks mike

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Design consideration