7

I know, that this question appears quite frequently in SO like here: but I would like to present some very specific example... I'm simply not sure if I make things right.

I've got a JDialog in which I can type some values, select some checkboxes... whatever... I've got also some Response object created in MyDialog which represents the MyDialog's "answer".

In JFrame which calls/creates JDialog:

MyDialog d = new MyDialog(this, ...);
d.showDialog();
// After MyDialog is closed (it's modal):
MyDialog.Response dialogResponse = d.getDialogResponse();
// Do something with response...

In Dialog (dialog can be closed by clicking "Save" button):

btnSave.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        dialogResponse = prepareResponse(); // prepares response on the basis of some data introduced by a user; dialogResponse is called from JFrame after Dialog is closed
        setVisible(false);
        dispose();  // <-- Important
    }
});

My question is: This solution works, I mean, the line MyDialog.Response dialogResponse = d.getDialogResponse(); returns proper values, but... if I close the dialog using dispose(), all dialog's resources can be garbage collected (don't have to... hard to predict, am I right?). So is it correct to retrieve my dialog's response it that way... Maybe in this case I should write only setVisible(false); without dispose().

Community
  • 1
  • 1
guitar_freak
  • 4,325
  • 6
  • 27
  • 43
  • if you need your `JDialog` later don't dispose it and just use `setVisible(false)`, otherwise you can dispose it without any problem – Paniz Aug 20 '13 at 08:38
  • Not sure I understand, hence the comment, but can't you dispose the Dialog from the Frame? – npinti Aug 20 '13 at 08:38
  • @npinti Yes, but I'm mainly curious if I can access my dialog's response after dispose() is invoked on Dialog – guitar_freak Aug 20 '13 at 08:43
  • 1
    If I understood your question in the right sense, why not make a class called `DialogResponse` as you fill values in your dialog, make necessary changes with your `DialogResponse` class's object and simply `return` this object to the parent `JFrame`, that way you really doesn't have to worry about `JDialog` being dispose or not, and moreover, you can use the same class for further updation, if the same `JDialog` is used again to get response. Though use this if your `JDialog` has huge to share with your parent class. – nIcE cOw Aug 20 '13 at 09:50
  • could u please help me on http://stackoverflow.com/questions/31652594/how-to-update-jtextfield-after-click-save-button/31652855#31652855 ? I already change the JFrame to JDialog. Now I have problem save the JDialog contents.. @guitar_freak – nani Jul 27 '15 at 17:29

4 Answers4

8

Quoted from the Javadocs:

The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifications between those actions).

So, your Response will be kept. All dispose() does is releasing the native screen resources, other members aren't marked for garbage collection.

Also, if you want to be extra sure, you could just call dispose() right after you retrieved your response object.

MarioP
  • 3,626
  • 1
  • 18
  • 29
3

if I close the dialog using dispose(), all dialog's resources can be garbage collected (don't have to... hard to predict, am I right?). So is it correct to retrieve my dialog's response it that way... Maybe in this case I should write only setVisible(false); without dispose().

Community
  • 1
  • 1
mKorbel
  • 108,320
  • 17
  • 126
  • 296
-1

why you don't use class variables (private static or public static) and use a factory method

  //it can be an object too public static Object  getResponseValue()
  public static Integer  getResponseValue(){
    myclassContainer container = new myclassContainer(someparent,modal).setvisible(true)
     return      Myfieldvalue
    }

    private static int Myfielvalue;

}
GoAntonio
  • 155
  • 2
  • 13
  • ??? clearly i missed a part here ....come on class variables are good they are necesary thats why we have them, factory methods constants like jdialogs butons you should read more – GoAntonio Mar 04 '14 at 01:04
-1
        dialog.add(BorderLayout.CENTER, tree_tmp);
        JButton confirm = new JButton("YES");
        confirm.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                confirm.setActionCommand("kkk");
                dialog.setVisible(false);
            }
        });
        dialog.add(BorderLayout.SOUTH,confirm);
        dialog.setSize(400, 400);
        dialog.setVisible(true);

        System.out.println(confirm.getActionCommand());