Closing the dialog created by JOptionPane.showOptionDialog ()

I am creating a parameter dialog using JOptionPane.showOptionDialog(...) ;

For the options parameter, I pass an array of JButtons, each with its own ActionListener .

One of these buttons closes the dialog box. My question is: what code do I put in the close button's event handler to close the options dialog?

Point that may matter: the class responsible for displaying this dialog box is single, and therefore the method responsible for displaying the dialog is static. Therefore, calling javax.swing.JInternalFrame.doDefaultCloseAction(); does not work "out of static context".

thanks

+4
source share
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); } } }); 
+14
source

Try

 JOptionPane.getRootFrame().dispose(); 
+4
source

Source: https://habr.com/ru/post/1308246/


All Articles