Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a dialog created by JOptionPane.showOptionDialog()

Tags:

java

swing

I am creating an options dialog using JOptionPane.showOptionDialog(...);

For the options parameter I am passing an array of JButtons each with its own ActionListener.

One of these buttons is responsible for closing the dialog. My question is: what code do I place in the close button's event handler to close the option dialog?

A point that may make a difference: the class responsible for showing this dialog is a singleton and, as such, the method responsible for displaying the dialog is static. Therefore, calling javax.swing.JInternalFrame.doDefaultCloseAction(); does not work "from a static context".

Thanks

like image 657
llm Avatar asked Nov 29 '22 05:11

llm


2 Answers

final JButton btn = new JButton("Close");

btn.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
    Window w = SwingUtilities.getWindowAncestor(btn);

    if (w != null) {
      w.setVisible(false);
    }
  }
});
like image 62
Adamski Avatar answered Dec 05 '22 10:12

Adamski


Try

JOptionPane.getRootFrame().dispose();   
like image 24
cmujica Avatar answered Dec 05 '22 10:12

cmujica