Search code examples
javajoptionpane

how to run JOptionPane object


I have this code to run a JOptionPane object

import import javax.swing.JOptionPane;
public class TheComboBoxes {
public static void main(String[] args) {
    JOptionPane optionPane = new JOptionPane(
            "The only way to close this dialog is by\n"
            + "pressing one of the following buttons.\n"
            + "Do you understand?",
            JOptionPane.QUESTION_MESSAGE,
            JOptionPane.YES_NO_OPTION);
    optionPane.createDialog("click");
}
}

however when i run the createDialog method nothing happens, how to i run the JOptionPane object properly?


Solution

  • import javax.swing.JDialog;
    import javax.swing.JOptionPane;
    public class TheComboBoxes {
        public static void main(String[] args) {
            JOptionPane optionPane = new JOptionPane("The only way to close this dialog is by\n"
                    + "pressing one of the following buttons.\n" + "Do you understand?", JOptionPane.QUESTION_MESSAGE,
                    JOptionPane.YES_NO_OPTION);
            JDialog dialog = optionPane.createDialog("click");
            dialog.setVisible(true);
        }
    }
    

    Use the JDialog to show your window