Results 1 to 5 of 5
- 04-04-2012, 02:19 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
pass data from Jdialog back to jFrame
Here is my scenario:
I would like to open a preferences jDialog from my jFrame. It will open on a button click.
The user will set preferences then submit them.
How do i get the data(3 integers) back to my jFrame so i can use the preferences they selected?
I have looked at several forums but none seem to have a clear answer.
-
Re: pass data from Jdialog back to jFrame
If the JDialog is modal, simply query its fields after it returns; that's it. If it is a non-modal dialog, then you'll have to add a WindowListener to it to know when it has been dealt with and is no longer visible. Then you would query its fields.
- 04-04-2012, 10:41 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: pass data from Jdialog back to jFrame
The JDialog is modal.
Do i put the return in when they hit the ok button to close preferences and go back to the jFrame?
I gave a crappy example of what i thought this may look like below.
Can you show me how i grab the returned value in the Jframe? Do i grab it where i set it to visible? I can't access the JDialogs radio buttons directly from the JFrame.
returnValue = jDialog.setVisible(true);
private void okayButtonActionPerformed(java.awt.event.ActionEve nt evt) {
// TODO add your handling code here:
this.setVisible(false);
okayPressed = true;
int selected;
if(jRadioButton1.isSelected())
{
selected = 1;
}
else if(jRadioButton2.isSelected())
{
selected = 2;
}
else
{
selected = 3;
}
return selected;
}
- 04-04-2012, 11:53 PM #4
Re: pass data from Jdialog back to jFrame
One thing you may not be aware of is that if a dialog is modal, then the thread that makes it visible (which should be the EDT) is suspended until the dialog becomes invisible. (Swing spawns a temporary EDT to continue pumping events.) Many dialogs are designed with a protected or private constructor and a public static showDialog(...) method that returns some value. Pseudocode for the showDialog method would look like this:
Java Code:static showDialog(parent, params) { create new instance modal to the parent set its fields using params make it visible thread resumes when dialog is closed get the dialog's fields and use them to set the return value }Last edited by kjkrum; 04-04-2012 at 11:58 PM.
Get in the habit of using standard Java naming conventions!
- 04-05-2012, 12:35 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: pass data from Jdialog back to jFrame
Yay i got it!!
I just created a public getPreferences() method in my jDialog and then in my JFrame int returnValue = prefJDialog.getPreferences();
So easy and yet it hung me up for so long. Thanks for your help ya'll.
I suppose if my actual fields were public instead of private i could've referenced them directly from Jframe as well.
Similar Threads
-
jDialog and JFrame Data Troubles
By yetibdhrana in forum New To JavaReplies: 1Last Post: 01-24-2012, 11:53 AM -
How to pass variable back from thread for animation
By blue50 in forum AWT / SwingReplies: 0Last Post: 12-02-2010, 04:54 AM -
to pass a parameter from a jframe children to its jframe mother
By anix in forum NetBeansReplies: 5Last Post: 06-14-2010, 06:10 PM -
Communicate JFrame with JDialog
By BeRniTo in forum AWT / SwingReplies: 2Last Post: 08-31-2009, 02:07 PM -
Returning data from a JFrame/JDialog?!
By Joe2003 in forum AWT / SwingReplies: 6Last Post: 01-08-2009, 12:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks