Example usage for javax.swing JOptionPane showOptionDialog

List of usage examples for javax.swing JOptionPane showOptionDialog

Introduction

In this page you can find the example usage for javax.swing JOptionPane showOptionDialog.

Prototype

@SuppressWarnings("deprecation")
public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType,
        int messageType, Icon icon, Object[] options, Object initialValue) throws HeadlessException 

Source Link

Document

Brings up a dialog with a specified icon, where the initial choice is determined by the initialValue parameter and the number of choices is determined by the optionType parameter.

Usage

From source file:Main.java

public static void main(String[] args) {
    int option = JOptionPane.showOptionDialog(null, "Title", "Message", JOptionPane.DEFAULT_OPTION,
            JOptionPane.PLAIN_MESSAGE, null, State.values(), State.values()[0]);
    if (option == JOptionPane.CLOSED_OPTION) {
        System.out.println("user closed the JOptionPane without selecting");
    } else {/*from ww  w .  j a v a2s.c o m*/
        State state = State.values()[option];
        doAction(state);
        System.out.println("code to do something based selected state");
    }
}

From source file:Main.java

public static void main(String args[]) {
    int n = JOptionPane.showOptionDialog(new JFrame(), "Message", "Title", JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Yes", "No" }, JOptionPane.YES_OPTION);

    if (n == JOptionPane.YES_OPTION) {
        System.out.println("Yes");
    } else if (n == JOptionPane.NO_OPTION) {
        System.out.println("No");
    } else if (n == JOptionPane.CLOSED_OPTION) {
        System.out.println("Closed by hitting the cross");
    }/*from w  ww.  ja  va 2 s  .  co m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    String[] buttons = { "Yes", "Yes to all", "No", "Cancel" };

    int rc = JOptionPane.showOptionDialog(null, "Question ?", "Confirmation", JOptionPane.WARNING_MESSAGE, 0,
            null, buttons, buttons[2]);//from w w  w . ja  v a2 s  .c  om

    System.out.println(rc);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame;//from   w  w w. ja  va 2 s  .  c o  m

    Object[] options = { "A", "B", "C", "D" };
    int choice = JOptionPane.showOptionDialog(null, "Nuovo Prodotto", "Scegli il prodotto",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
    if (choice == 0) {
        System.out.println("0 selected");
    } else {
        System.out.println("Something else selected");
    }
}

From source file:Main.java

public static void main(String[] args) {
    Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    Object[] possibilities = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    Integer i = (Integer) JOptionPane.showOptionDialog(null, null, "ShowInputDialog", JOptionPane.PLAIN_MESSAGE,
            1, errorIcon, possibilities, 0);

    // or//from  w  w w  .  j a v  a  2  s.c  o  m

    Integer ii = (Integer) JOptionPane.showInputDialog(null, "Select number:\n\from JComboBox",
            "ShowInputDialog", JOptionPane.PLAIN_MESSAGE, errorIcon, possibilities, "Numbers");

}

From source file:Main.java

public static void main(String[] args) {
    Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    Object[] possibilities = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
    Integer i = (Integer) JOptionPane.showOptionDialog(null, null, "ShowInputDialog", JOptionPane.PLAIN_MESSAGE,
            1, errorIcon, possibilities, 0);
    Integer ii = (Integer) JOptionPane.showInputDialog(null, "Select number:\n\from JComboBox",
            "ShowInputDialog", JOptionPane.PLAIN_MESSAGE, errorIcon, possibilities, "Numbers");
}

From source file:StringArrayOptionPopups.java

public static void main(String[] a) {
    JFrame frame = new JFrame();

    Icon blueIcon = new ImageIcon("yourFile.gif");
    Object stringArray[] = { "Do It", "No Way" };
    JOptionPane.showOptionDialog(frame, "Continue printing?", "Select an Option", JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE, blueIcon, stringArray, stringArray[0]);
}

From source file:MainClass.java

public static void main(String[] a) {
    Icon blueIcon = new MyIcon(Color.BLUE);
    Object stringArray[] = { "Do It", "No Way" };
    JOptionPane.showOptionDialog(new JDesktopPane(), "Continue printing?", "Select an Option",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, blueIcon, stringArray, stringArray[0]);

}

From source file:AddingIconsToOptionPopups.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    Icon greenIcon = new ImageIcon("yourFile.gif");
    Icon redIcon = new ImageIcon("");
    Object iconArray[] = { greenIcon, redIcon };

    JOptionPane.showOptionDialog(frame, "Continue printing?", "Select an Option", JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, iconArray, iconArray[1]);

}

From source file:Main.java

public static void main(String[] args) {
    String[] weekdays = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
    final JComboBox<String> combo = new JComboBox<>(weekdays);

    String[] options = { "OK", "Cancel", "Help" };

    String title = "Title";
    int selection = JOptionPane.showOptionDialog(null, combo, title, JOptionPane.DEFAULT_OPTION,
            JOptionPane.PLAIN_MESSAGE, null, options, options[0]);

    if (selection > 0) {
        System.out.println("selection is: " + options[selection]);
    }//from w w  w  . j av a  2s.com

    Object weekday = combo.getSelectedItem();
    if (weekday != null) {
        System.out.println("weekday: " + weekday);
    }

}