Example usage for javax.swing JOptionPane JOptionPane

List of usage examples for javax.swing JOptionPane JOptionPane

Introduction

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

Prototype

public JOptionPane(Object message, int messageType, int optionType) 

Source Link

Document

Creates an instance of JOptionPane to display a message with the specified message type and options.

Usage

From source file:MainClass.java

public static void main(String[] a) {
    JOptionPane optionPane = new JOptionPane("Continue printing?", JOptionPane.QUESTION_MESSAGE,
            JOptionPane.YES_NO_OPTION);
    JDialog dialog = optionPane.createDialog(null, "Manual Creation");
    dialog.setVisible(true);//from   ww w  .j  a  va2s. c om
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JOptionPane pane = new JOptionPane("your message", JOptionPane.ERROR_MESSAGE, JOptionPane.OK_OPTION);
    JDialog d = pane.createDialog(null, "title");
    d.pack();// w  w  w .j ava 2  s . c  o  m
    d.setModal(false);
    d.setVisible(true);
    while (pane.getValue() == JOptionPane.UNINITIALIZED_VALUE) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ie) {
        }
    }
    System.exit(0);
}

From source file:CreateDialogFromOptionPane.java

public static void main(final String[] args) {
    JFrame parent = new JFrame();
    JOptionPane optionPane = new JOptionPane("Continue printing?", JOptionPane.QUESTION_MESSAGE,
            JOptionPane.YES_NO_OPTION);
    JDialog dialog = optionPane.createDialog(parent, "Manual Creation");
    dialog.setVisible(true);// w  w  w  . j  av a  2  s.c  o m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JOptionPane jop = new JOptionPane("Message", JOptionPane.QUESTION_MESSAGE, JOptionPane.DEFAULT_OPTION);

    JDialog dialog = jop.createDialog("Dialog Title");

    Image image = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    dialog.setIconImage(image);//w w  w  .  j  av a 2s  .com
    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    JOptionPane pane = new JOptionPane("Some message", JOptionPane.QUESTION_MESSAGE,
            JOptionPane.YES_NO_OPTION) {
        @Override/* ww  w. j a v  a  2  s  .  c o  m*/
        public void setValue(Object newValue) {
            super.setValue(newValue);
            JOptionPane.showMessageDialog(frame.getContentPane(), "You have hit " + newValue);
        }
    };
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JLabel("Some panel in the middle"), BorderLayout.CENTER);
    frame.add(pane, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:ManualDisplayPopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("NoButton Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Ask");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            JOptionPane optionPane = new JOptionPane("Continue printing?", JOptionPane.QUESTION_MESSAGE,
                    JOptionPane.YES_NO_OPTION);
            JDialog dialog = optionPane.createDialog(source, "Manual Creation");
            dialog.show();/*from   www  .  j a  v a  2s . co  m*/
            int selection = OptionPaneUtils.getSelection(optionPane);
            System.out.println(selection);
        }
    };
    button.addActionListener(actionListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    options.add(createOptionPane("Plain Message", JOptionPane.PLAIN_MESSAGE));
    options.add(createOptionPane("Error Message", JOptionPane.ERROR_MESSAGE));
    options.add(createOptionPane("Information Message", JOptionPane.INFORMATION_MESSAGE));
    options.add(createOptionPane("Warning Message", JOptionPane.WARNING_MESSAGE));
    options.add(createOptionPane("Want to do something?", JOptionPane.QUESTION_MESSAGE));
    items = new Object[] { "First", "Second", "Third" };
    JComboBox choiceCombo = new JComboBox(items);
    options.add(new JOptionPane(choiceCombo, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION),
            "Question Message");
    JFrame frame = new JFrame("JOptionPane'Options");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(options, BorderLayout.CENTER);
    frame.pack();//ww w  . j  av  a  2s.com
    frame.setVisible(true);
}

From source file:com.gs.obevo.util.inputreader.DialogInputReader.java

@Override
public String readLine(String promptMessage) {
    final JTextField juf = new JTextField();
    JOptionPane juop = new JOptionPane(juf, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
    JDialog userDialog = juop.createDialog(promptMessage);
    userDialog.addComponentListener(new ComponentAdapter() {
        @Override/*  w  w  w .  ja  v  a  2 s .  c o  m*/
        public void componentShown(ComponentEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    juf.requestFocusInWindow();
                }
            });
        }
    });
    userDialog.setVisible(true);
    int uresult = (Integer) juop.getValue();
    userDialog.dispose();
    String userName = null;
    if (uresult == JOptionPane.OK_OPTION) {
        userName = new String(juf.getText());
    }

    if (StringUtils.isEmpty(userName)) {
        return null;
    } else {
        return userName;
    }
}

From source file:com.gs.obevo.util.inputreader.DialogInputReader.java

@Override
public String readPassword(String promptMessage) {
    final JPasswordField jpf = new JPasswordField();
    JOptionPane jop = new JOptionPane(jpf, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
    JDialog dialog = jop.createDialog(promptMessage);
    dialog.addComponentListener(new ComponentAdapter() {
        @Override/*from   w  ww  . ja  v  a 2 s  .c o  m*/
        public void componentShown(ComponentEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    jpf.requestFocusInWindow();
                }
            });
        }
    });
    dialog.setVisible(true);
    int result = (Integer) jop.getValue();
    dialog.dispose();
    String password = null;
    if (result == JOptionPane.OK_OPTION) {
        password = new String(jpf.getPassword());
    }
    if (StringUtils.isEmpty(password)) {
        return null;
    } else {
        return password;
    }
}

From source file:OptPaneComparison.java

public OptPaneComparison(final String message) {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    final int msgType = JOptionPane.QUESTION_MESSAGE;
    final int optType = JOptionPane.OK_CANCEL_OPTION;
    final String title = message;

    setSize(350, 200);/* ww  w.  j  a v a  2 s.c om*/

    // Create a desktop for internal frames
    final JDesktopPane desk = new JDesktopPane();
    setContentPane(desk);

    // Add a simple menu bar
    JMenuBar mb = new JMenuBar();
    setJMenuBar(mb);

    JMenu menu = new JMenu("Dialog");
    JMenu imenu = new JMenu("Internal");
    mb.add(menu);
    mb.add(imenu);
    final JMenuItem construct = new JMenuItem("Constructor");
    final JMenuItem stat = new JMenuItem("Static Method");
    final JMenuItem iconstruct = new JMenuItem("Constructor");
    final JMenuItem istat = new JMenuItem("Static Method");
    menu.add(construct);
    menu.add(stat);
    imenu.add(iconstruct);
    imenu.add(istat);

    // Create our JOptionPane. We're asking for input, so we call
    // setWantsInput.
    // Note that we cannot specify this via constructor parameters.
    optPane = new JOptionPane(message, msgType, optType);
    optPane.setWantsInput(true);

    // Add a listener for each menu item that will display the appropriate
    // dialog/internal frame
    construct.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {

            // Create and display the dialog
            JDialog d = optPane.createDialog(desk, title);
            d.setVisible(true);

            respond(getOptionPaneValue());
        }
    });

    stat.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            String s = JOptionPane.showInputDialog(desk, message, title, msgType);
            respond(s);
        }
    });

    iconstruct.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {

            // Create and display the dialog
            JInternalFrame f = optPane.createInternalFrame(desk, title);
            f.setVisible(true);

            // Listen for the frame to close before getting the value from
            // it.
            f.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent ev) {
                    if ((ev.getPropertyName().equals(JInternalFrame.IS_CLOSED_PROPERTY))
                            && (ev.getNewValue() == Boolean.TRUE)) {
                        respond(getOptionPaneValue());
                    }
                }
            });
        }
    });

    istat.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            String s = JOptionPane.showInternalInputDialog(desk, message, title, msgType);
            respond(s);
        }
    });
}