0

I am using a JDialog to get payment info, paymentAmount and Date is submitted by a JTextfield and a datechooser.beans.DateChooserCombo.

When the user close the JDialog or clicks Cancel, JDialog closes.But when they click the Payment button and JDialog reappears, previously submitted inputs are shown.

I want JDialog to be default state whenever it appears.Is there a default way to do this, or i have to create my own reset method?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69

2 Answers2

3

When you close a dialog it is not destroyed. It will just become invisible, but it still contains everything as it was when it was closed.

You may override the function setVisible() and reinitialize it if the dialog should be shown again.

 @Override
 public void setVisible(boolean bVisible)
 {
     if(bVisible == false)
     {
         super.setVisible(bVisible);
         return;
     }

     initMyValues();
     super.setVisible(bVisible);
     return;
 }

Alternatively you could create a WindowListener and then you get notified about various state changes of the window. Depends on what suits your needs better. The WindowListener doesn't require you to create a separate class, jsut to override the setVisible(), but you have to add some extra function required by the interface.

Devolus
  • 21,661
  • 13
  • 66
  • 113
3

Another workaround would be to set a windowListener to your dialog.

myDialog.addWindowListener(new WindowListener() {
            /*Implements over methods here*/
            @Override
            public void windowClosing(WindowEvent e) {
                //set default values here
            }});
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • NetBeans auto-generates this kind of code when you create a new JDialog (small difference: it registers a WindowAdapter instead of WindowListener). Could you not also call dispose() in your Payment button event handler after you finished? That was recommended in a similar question: http://stackoverflow.com/questions/7256606/jdialog-setvisiblefalse-vs-dispose – reggoodwin Jun 15 '13 at 20:50