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, Icon icon, Object[] options,
        Object initialValue) 

Source Link

Document

Creates an instance of JOptionPane to display a message with the specified message type, icon, and options, with the initially-selected option specified.

Usage

From source file:Main.java

public static void main(String... args) {
    JOptionPane optionPane = new JOptionPane("Its me", JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION,
            null, null, "Please ENTER your NAME here");
    optionPane.setWantsInput(true);/*  ww  w  .  j  a  v  a 2 s  . co  m*/
    JDialog dialog = optionPane.createDialog(null, "TEST");
    dialog.setLocation(10, 20);
    dialog.setVisible(true);
    System.out.println(optionPane.getInputValue());

}

From source file:Main.java

public static void main(String[] args) {
    String[] options = { "Button 1", "Button 2", "Button 3" };

    JOptionPane myOptionPane = new JOptionPane("Heres a test message", JOptionPane.QUESTION_MESSAGE,
            JOptionPane.YES_NO_OPTION, null, options, options[2]);
    JDialog myDialog = myOptionPane.createDialog(null, "My Test");
    myDialog.setModal(true);/*w w  w.j  a  v  a2  s.com*/

    inactivateOption(myDialog, options[1]);

    myDialog.setVisible(true);
    Object result = myOptionPane.getValue();
    System.out.println("result: " + result);

}

From source file:Main.java

private void displayGUI() {
    JOptionPane optionPane = new JOptionPane(getPanel(), JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION,
            null, new Object[] {}, null);
    dialog = optionPane.createDialog("import");
    dialog.setVisible(true);//from ww  w .ja  v a 2  s .c om
}

From source file:components.CustomDialog.java

/** Creates the reusable dialog. */
public CustomDialog(Frame aFrame, String aWord, DialogDemo parent) {
    super(aFrame, true);
    dd = parent;/*from w ww .  ja v a 2 s  .  c o m*/

    magicWord = aWord.toUpperCase();
    setTitle("Quiz");

    textField = new JTextField(10);

    //Create an array of the text and components to be displayed.
    String msgString1 = "What was Dr. SEUSS's real last name?";
    String msgString2 = "(The answer is \"" + magicWord + "\".)";
    Object[] array = { msgString1, msgString2, textField };

    //Create an array specifying the number of dialog buttons
    //and their text.
    Object[] options = { btnString1, btnString2 };

    //Create the JOptionPane.
    optionPane = new JOptionPane(array, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options,
            options[0]);

    //Make this dialog display it.
    setContentPane(optionPane);

    //Handle window closing correctly.
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent we) {
            /*
             * Instead of directly closing the window,
             * we're going to change the JOptionPane's
             * value property.
             */
            optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
        }
    });

    //Ensure the text field always gets the first focus.
    addComponentListener(new ComponentAdapter() {
        public void componentShown(ComponentEvent ce) {
            textField.requestFocusInWindow();
        }
    });

    //Register an event handler that puts the text into the option pane.
    textField.addActionListener(this);

    //Register an event handler that reacts to option pane state changes.
    optionPane.addPropertyChangeListener(this);
}

From source file:edu.scripps.fl.pubchem.xmltool.gui.GUIComponent.java

public void openPDF(boolean isInternalR, File pdf, Component comp) throws FileNotFoundException {
    if (isInternalR) {
        try {/* ww w  .j a  va  2s .  co m*/
            Desktop.getDesktop().open(pdf);
        } catch (Exception ex) {
            log.info(ex.getMessage());
            String msg = "Unable to open PDF format through java. Would you like to save "
                    + pdf.getAbsoluteFile() + " in a new location?";
            JOptionPane pane = new JOptionPane(msg, JOptionPane.QUESTION_MESSAGE,
                    JOptionPane.YES_NO_CANCEL_OPTION, null, null, JOptionPane.YES_OPTION);
            JDialog dialog = pane.createDialog(comp, "Report PDF");
            dialog.setVisible(true);
            Object choice = pane.getValue();
            if (choice.equals(JOptionPane.YES_OPTION)) {
                jfcFiles = new JFileChooser();
                jfcFiles.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                int state = jfcFiles.showSaveDialog(comp);

                if (state == JFileChooser.APPROVE_OPTION) {
                    File file = checkFileExtension(".pdf");
                    log.info("Opening: " + file.getName() + ".");
                    if (file != null)
                        pdf.renameTo(file);
                } else {
                    log.info("Open command cancelled by user.");
                }
            }
        }
    }
}

From source file:eu.delving.sip.base.VisualFeedback.java

public static String askQuestion(JDesktopPane desktop, String question, Object initialSelectionValue) {
    final JOptionPane pane = new JOptionPane(question, QUESTION_MESSAGE, OK_CANCEL_OPTION, null, null, null);
    pane.putClientProperty(new Object(), Boolean.TRUE);
    pane.setWantsInput(true);//from w ww.j av a  2  s. com
    pane.setInitialSelectionValue(initialSelectionValue);
    JDialog frame = pane.createDialog(desktop, "Question");
    pane.selectInitialValue();
    frame.setVisible(true);
    acquireFocus();
    Object value = pane.getInputValue();
    return value == UNINITIALIZED_VALUE ? null : (String) value;
}

From source file:eu.delving.sip.base.VisualFeedback.java

public static boolean askOption(JDesktopPane desktop, Object message, String title, int optionType,
        int messageType) {
    JOptionPane pane = new JOptionPane(message, messageType, optionType, null, null, null);
    pane.putClientProperty(new Object(), Boolean.TRUE);
    JDialog frame = pane.createDialog(desktop, title);
    pane.selectInitialValue();/*w  ww  .ja  v a 2s  . co m*/
    frame.setVisible(true);
    acquireFocus();
    Object selectedValue = pane.getValue();
    return selectedValue != null && selectedValue instanceof Integer && (Integer) selectedValue == YES_OPTION;
}

From source file:net.pms.newgui.LanguageSelection.java

public void show() {
    if (PMS.isHeadless()) {
        // Can only get here during startup in headless mode, there's no way to trigger it from the GUI
        LOGGER.info(//from www .ja v a2  s.  c  o m
                "No language is configured and the language selection dialog is unavailable in headless mode");
        LOGGER.info("Defaulting to OS locale {}", Locale.getDefault().getDisplayName());
        PMS.setLocale(Locale.getDefault());
    } else {
        pane = new JOptionPane(buildComponent(), JOptionPane.PLAIN_MESSAGE, JOptionPane.NO_OPTION, null,
                new JButton[] { applyButton, selectButton }, selectButton);
        pane.setComponentOrientation(ComponentOrientation.getOrientation(locale));
        dialog = pane.createDialog(parentComponent, PMS.NAME);
        dialog.setModalityType(ModalityType.APPLICATION_MODAL);
        dialog.setIconImage(LooksFrame.readImageIcon("icon-32.png").getImage());
        setStrings();
        dialog.pack();
        dialog.setLocationRelativeTo(parentComponent);
        dialog.setVisible(true);
        dialog.dispose();

        if (pane.getValue() == null) {
            aborted = true;
        } else if (!((String) pane.getValue()).equals(PMS.getConfiguration().getLanguageRawString())) {
            if (rebootOnChange) {
                int response = JOptionPane.showConfirmDialog(parentComponent,
                        String.format(buildString("Dialog.Restart", true), PMS.NAME, PMS.NAME),
                        buildString("Dialog.Confirm"), JOptionPane.YES_NO_CANCEL_OPTION);
                if (response != JOptionPane.CANCEL_OPTION) {
                    PMS.getConfiguration().setLanguage((String) pane.getValue());
                    if (response == JOptionPane.YES_OPTION) {
                        try {
                            PMS.getConfiguration().save();
                        } catch (ConfigurationException e) {
                            LOGGER.error("Error while saving configuration: {}", e.getMessage());
                            LOGGER.trace("", e);
                        }
                        ProcessUtil.reboot();
                    }
                }
            } else {
                PMS.getConfiguration().setLanguage((String) pane.getValue());
            }
        }
    }
}

From source file:com.hexidec.ekit.component.PropertiesDialog.java

public PropertiesDialog(Window parent, String[] fields, String[] types, String[] values, String title,
        boolean bModal) {
    super(parent, title);
    setModal(bModal);//from w  w w  . ja  va  2s  .  c om
    htInputFields = new Hashtable<String, JComponent>();
    final Object[] buttonLabels = { Translatrix.getTranslationString("DialogAccept"),
            Translatrix.getTranslationString("DialogCancel") };
    List<Object> panelContents = new ArrayList<Object>();
    for (int iter = 0; iter < fields.length; iter++) {
        String fieldName = fields[iter];
        String fieldType = types[iter];
        JComponent fieldComponent;
        JComponent panelComponent = null;
        if (fieldType.equals("text") || fieldType.equals("integer")) {
            fieldComponent = new JTextField(3);
            if (values[iter] != null && values[iter].length() > 0) {
                ((JTextField) (fieldComponent)).setText(values[iter]);
            }

            if (fieldType.equals("integer")) {
                ((AbstractDocument) ((JTextField) (fieldComponent)).getDocument())
                        .setDocumentFilter(new DocumentFilter() {

                            @Override
                            public void insertString(FilterBypass fb, int offset, String text,
                                    AttributeSet attrs) throws BadLocationException {
                                replace(fb, offset, 0, text, attrs);
                            }

                            @Override
                            public void replace(FilterBypass fb, int offset, int length, String text,
                                    AttributeSet attrs) throws BadLocationException {

                                if (StringUtils.isNumeric(text)) {
                                    super.replace(fb, offset, length, text, attrs);
                                }

                            }

                        });
            }
        } else if (fieldType.equals("bool")) {
            fieldComponent = new JCheckBox(fieldName);
            if (values[iter] != null) {
                ((JCheckBox) (fieldComponent)).setSelected(values[iter] == "true");
            }
            panelComponent = fieldComponent;
        } else if (fieldType.equals("combo")) {
            fieldComponent = new JComboBox();
            if (values[iter] != null) {
                StringTokenizer stParse = new StringTokenizer(values[iter], ",", false);
                while (stParse.hasMoreTokens()) {
                    ((JComboBox) (fieldComponent)).addItem(stParse.nextToken());
                }
            }
        } else {
            fieldComponent = new JTextField(3);
        }
        htInputFields.put(fieldName, fieldComponent);
        if (panelComponent == null) {
            panelContents.add(fieldName);
            panelContents.add(fieldComponent);
        } else {
            panelContents.add(panelComponent);
        }
    }
    jOptionPane = new JOptionPane(panelContents.toArray(), JOptionPane.QUESTION_MESSAGE,
            JOptionPane.OK_CANCEL_OPTION, null, buttonLabels, buttonLabels[0]);

    setContentPane(jOptionPane);
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

    jOptionPane.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            String prop = e.getPropertyName();
            if (isVisible() && (e.getSource() == jOptionPane) && (prop.equals(JOptionPane.VALUE_PROPERTY)
                    || prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
                Object value = jOptionPane.getValue();
                if (value == JOptionPane.UNINITIALIZED_VALUE) {
                    return;
                }
                if (value.equals(buttonLabels[0])) {
                    setVisible(false);
                } else {
                    setVisible(false);
                }
            }
        }
    });
    this.pack();
    setLocation(SwingUtilities.getPointForCentering(this, parent));
}

From source file:net.menthor.editor.v2.util.Util.java

/** Helper method for constructing an always-on-top modal dialog. */
public static Object show(String title, int type, Object message, Object[] options, Object initialOption) {
    if (options == null) {
        options = new Object[] { "Ok" };
        initialOption = "Ok";
    }/*from   w  w w. ja v a  2s . c  om*/
    JOptionPane p = new JOptionPane(message, type, JOptionPane.DEFAULT_OPTION, null, options, initialOption);
    p.setInitialValue(initialOption);
    JDialog d = p.createDialog(null, title);
    p.selectInitialValue();
    d.setAlwaysOnTop(true);
    d.setVisible(true);
    d.dispose();
    return p.getValue();
}