0

I have a jdialog and want close it on confirmation after that store the data of a text box... now I have no problem to store the data from the box but,

How can I close this dialog after the operation???

Seems a simple thing but I haven't found the solution.

public class test extends JDialog {

    private final JPanel contentPanel = new JPanel();

    public test() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
                             okButton.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        try{

                            int x=Integer.parseInt(textField.getText());
                            saver.saveN(x);

                        }catch(Exception ecc){
                            JOptionPane.showMessageDialog(Test.this,"error");
                        }
                    }
                });
            }

        }
    }

}
AndreaF
  • 11,975
  • 27
  • 102
  • 168

3 Answers3

3

Either use Window#dispose or Window#setVisible(false).

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • I cannot use dispose() on this element and if I use contentPanel.setVisible(false); hide all the contents of the dialog but the windows remains opened... what is wrong?? – AndreaF Apr 07 '12 at 20:28
  • @AndreaF You are calling the methods on the `contentPanel`, not the `JDialog`. Since your class extends from `JDialog`, call `this.dispose()` or `this.setVisible(false)`. – Jeffrey Apr 07 '12 at 20:33
  • I cannot use this.dispose() or this.setVisible(false) from the OK listener quoted in the first post – AndreaF Apr 07 '12 at 20:37
  • @AndreaF Since you are in an anonymous class, you have to qualify `this` with the name of the enclosing class. The method call would become `test.this.dispose()` or `test.this.setVisible(false)`. – Jeffrey Apr 07 '12 at 20:39
1
  • if you use this dialog only once time then there is same to use dispose() as setVisible(false)

  • in the case that you invoke this method more than once time, then you can use HIDE_ON_CLOSE or setVisible(false), better would be re_use this JDialog

EDIT

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class Test {

    private static final long serialVersionUID = 1L;
    private JDialog dialog = new JDialog();
    private final JPanel contentPanel = new JPanel();
    private Timer timer1;
    private JButton killkButton = new JButton("Kill JDialog");

    public Test() {
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

        JPanel buttonPane = new JPanel();
        JButton okButton = new JButton("OK");
        okButton.setActionCommand("OK");
        buttonPane.add(okButton);

        killkButton.setActionCommand("Kill JDialog");
        buttonPane.add(killkButton);

        dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        dialog.addWindowListener(new WindowListener() {

            public void windowOpened(WindowEvent e) {
            }

            public void windowClosing(WindowEvent e) {
                startTimer();
            }

            public void windowClosed(WindowEvent e) {
            }

            public void windowIconified(WindowEvent e) {
            }

            public void windowDeiconified(WindowEvent e) {
            }

            public void windowActivated(WindowEvent e) {
            }

            public void windowDeactivated(WindowEvent e) {
            }
        });
        dialog.setLayout(new BorderLayout());
        dialog.getRootPane().setDefaultButton(okButton);
        dialog.add(buttonPane, BorderLayout.SOUTH);
        dialog.add(contentPanel, BorderLayout.CENTER);
        dialog.pack();
        dialog.setLocation(100, 100);
        dialog.setVisible(true);
        setKeyBindings();
    }

    private void setKeyBindings() {
        killkButton.getInputMap(
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                KeyStroke.getKeyStroke("ENTER"), "clickENTER");
        killkButton.getActionMap().put("clickENTER", new AbstractAction() {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    private void startTimer() {
        timer1 = new Timer(1000, new AbstractAction() {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        dialog.setVisible(true);
                    }
                });
            }
        });
        timer1.setDelay(500);
        timer1.setRepeats(false);
        timer1.start();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Test test = new Test();
            }
        });
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

If you plan to use a jDialog again with all the field values and component states left the same when you close it, use setVisible(false).

In any other case, call dispose(), to avoid the jDialog remaining in the memory when it is no longer required.

MarioDS
  • 12,895
  • 15
  • 65
  • 121
  • if you want to do it from inside the Test class, do `this.dispose()`. In case you have a Test instance do `instance.dispose()`. – MarioDS Apr 07 '12 at 20:37
  • from the listener -this- refers to the listener not to the JDialog, so if I try to use this.dispose() get a compilation error – AndreaF Apr 07 '12 at 20:40
  • Oops, my bad. Define the code the actionListener does in a new method in the test class and you will be fine using "this". Otherwise use the same as you did when you catch the exception "Test.this.dispose()" but I don't think that works tbh... I'd remove that from your exception too, which will be in your new method anyway. – MarioDS Apr 07 '12 at 20:56