File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes passing text from JDialog to JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "passing text from JDialog to JFrame" Watch "passing text from JDialog to JFrame" New topic
Author

passing text from JDialog to JFrame

Arnold Charming
Greenhorn

Joined: Sep 02, 2004
Posts: 6
Hi!

I'm new to this forum and also quite new to Java.
I'm just playing around with it and came to a problem. I have a Jrame window width JDialog popping out of it when user click on a Jbutton. But now I want to send some text from JDialog to Jframe. How to do this? I've tried several things but no luck Help is needed!
Jez Nicholson
Ranch Hand

Joined: Oct 01, 2002
Posts: 39
Quick route is to use JOptionPane to create your dialogs and it will return values.

String inputValue = JOptionPane.showInputDialog("Please input a value");

see the javadoc for more examples. http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html

if you need to use JDialog directly then you might have to add a PropertyChangeListener. The Java Tutorial can help http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
Arnold Charming
Greenhorn

Joined: Sep 02, 2004
Posts: 6
Hi!

I'm not quite sure this is what I'm looking for. In my JDialog, a user has to click a button to send text to parent window (JFrame). Text is then displayed inside a JLabell. It tried something like this (this is in JDialog class):

jLabel1.setText("Bla Bla");

jLabel1 is defined on JFrame. This code is executed when someone clicks on a button inside JDialog.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535

Another option is to send the label reference along to the launchDialog method. The changes for this are:

This keeps label out of class scope, ie, label is now a local variable instead of a member variable.
Jez Nicholson
Ranch Hand

Joined: Oct 01, 2002
Posts: 39
Yep.

The choice between instantiating a JDialog or getting it from JOptionPane is about how fancy your dialog needs to be....

Arnold Charming
Greenhorn

Joined: Sep 02, 2004
Posts: 6
Hi!

The greenhorn is back
I don't know in which GUI editor you built this but my Jbuilder X Foundation is not the same. The Borland makes for every different frame ,dialog it's own class file. Because my Jdialog in is dialog1.class and main frame is in Frame1.class I can't access to variables that are in Frame1.class. How can do this? Does Java support some cained of a global variable? One more thing. What editors do you guys use? Don't tell me, you're writting your GUI code by hand?
Arnold Charming
Greenhorn

Joined: Sep 02, 2004
Posts: 6
Hi!

I put static before JLabel and it works. But somehow am confused if this is the right way. Can somehow confirm this?
Mike Southgate
Ranch Hand

Joined: Jul 18, 2003
Posts: 183
To pass info from a dialog back to it's parent frame, what I do is instatiate the jdialog, show it, hide it (this is in the listener for the jdialog's done button). It's important not to dispose of the dialog but only hide it. That way when your jframe gets controll back it can just call a public getter method that you wrote as part of your dialog. You can dispose of the dialog after that.

ms


ms<br />SCJP, SCJD
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
One of the fun ares in java is in how you put things together. How will your two classes communicate/work together. To launch the dialog in the DialogClass your JFrame class needs a reference (dialogClass in the demo below) to the DialogClass. So how do you get the text back to the JFrame class once the dialog is launched from within the DialogClass? In the code below the DialogClass uses it's new reference to the caller, ie, the DialogCommunication reference dialogCommunication that was passed in through it's constructor, to send the text back to the JFrame class.

There are many ways to put this together. With practice and experimentation we each find our own favorite ways. The main thing is to get the info moved without exposing too much in the process.

And yes I write code by hand in a text editor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing text from JDialog to JFrame