Position | Description |
CANCEL_OPTION | Used when the Cancel button is pressed |
CLOSED_OPTION | Used when the pop-up window closed without the user pressing a button |
NO_OPTION | Used when the No button is pressed |
OK_OPTION | Used when the OK button is pressed |
YES_OPTION | Used when the Yes button is pressed |
import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class Test extends JFrame { public Test() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(new JLabel("Placeholder label")); pack(); setSize(200, 200); setVisible(true); int replaced = JOptionPane.showConfirmDialog(this, "Replace existing selection?"); String result = "?"; switch (replaced) { case JOptionPane.CANCEL_OPTION: result = "Canceled"; break; case JOptionPane.CLOSED_OPTION: result = "Closed"; break; case JOptionPane.NO_OPTION: result = "No"; break; case JOptionPane.YES_OPTION: result = "Yes"; break; default: ; } System.out.println("Replace? " + result); } public static void main(String[] args) { new Test(); } }