Salih Erikci
Salih Erikci

Reputation: 5087

Resetting a JDialog after closing

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?

Upvotes: 0

Views: 1524

Answers (2)

Alexis C.
Alexis C.

Reputation: 93842

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

Upvotes: 3

Devolus
Devolus

Reputation: 22074

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.

Upvotes: 3

Related Questions