Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

java - Closing a dialog created by JOptionPane.showOptionDialog()

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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);
    }
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...