• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing text from JDialog to JFrame

 
Arnold Charming
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep.

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

 
Arnold Charming
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic