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!
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 is defined on JFrame. This code is executed when someone clicks on a button inside JDialog.
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.
Joined: Oct 01, 2002
The choice between instantiating a JDialog or getting it from JOptionPane is about how fancy your dialog needs to be....
Joined: Sep 02, 2004
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?
Joined: Sep 02, 2004
I put static before JLabel and it works. But somehow am confused if this is the right way. Can somehow confirm this?
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<br />SCJP, SCJD
Joined: Jan 14, 2004
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.