Results 1 to 5 of 5
  1. #1
    Rogue45 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Exclamation 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Rogue45 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default 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;
    }

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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-05-2012 at 12:58 AM.
    Get in the habit of using standard Java naming conventions!

  5. #5
    Rogue45 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default 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

  1. jDialog and JFrame Data Troubles
    By yetibdhrana in forum New To Java
    Replies: 1
    Last Post: 01-24-2012, 12:53 PM
  2. Replies: 0
    Last Post: 12-02-2010, 05:54 AM
  3. Replies: 5
    Last Post: 06-14-2010, 07:10 PM
  4. Communicate JFrame with JDialog
    By BeRniTo in forum AWT / Swing
    Replies: 2
    Last Post: 08-31-2009, 03:07 PM
  5. Returning data from a JFrame/JDialog?!
    By Joe2003 in forum AWT / Swing
    Replies: 6
    Last Post: 01-08-2009, 01:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •