Java Code Examples for javax.swing.JOptionPane#setInitialValue()

The following examples show how to use javax.swing.JOptionPane#setInitialValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OurDialog.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for constructing an always-on-top modal dialog.
 */
private static Object show(JFrame parent, String title, int type, Object message, Object[] options, Object initialOption) {
    if (options == null) {
        options = new Object[] {
                                "Ok"
        };
        initialOption = "Ok";
    }
    JOptionPane p = new JOptionPane(message, type, JOptionPane.DEFAULT_OPTION, null, options, initialOption);
    p.setInitialValue(initialOption);
    JDialog d = p.createDialog(parent, title);
    p.selectInitialValue();
    d.setAlwaysOnTop(true);
    d.setVisible(true);
    d.dispose();
    return p.getValue();
}
 
Example 2
Source File: WindowUtils.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Shows an option dialog.
 *
 * @param parentComponent The parent {@link Component} to use. May be {@code null}.
 * @param message         The message. May be a {@link Component}.
 * @param title           The title to use.
 * @param resizable       Whether to allow the dialog to be resized by the user.
 * @param optionType      The type of option dialog. Use the {@link JOptionPane} constants.
 * @param messageType     The type of message. Use the {@link JOptionPane} constants.
 * @param icon            The icon to use. May be {@code null}.
 * @param options         The options to display. May be {@code null}.
 * @param initialValue    The initial option.
 * @return See the documentation for {@link JOptionPane}.
 */
public static int showOptionDialog(Component parentComponent, Object message, String title, boolean resizable, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) {
    JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, options, initialValue);
    pane.setUI(new SizeAwareBasicOptionPaneUI(pane.getUI()));
    pane.setInitialValue(initialValue);
    pane.setComponentOrientation((parentComponent == null ? JOptionPane.getRootFrame() : parentComponent).getComponentOrientation());

    JDialog dialog = pane.createDialog(getWindowForComponent(parentComponent), title);
    WindowSizeEnforcer.monitor(dialog);
    pane.selectInitialValue();
    dialog.setResizable(resizable);
    Component field = getFirstFocusableField(message);
    if (field != null) {
        dialog.addWindowFocusListener(new WindowAdapter() {
            @Override
            public void windowGainedFocus(WindowEvent event) {
                field.requestFocus();
                dialog.removeWindowFocusListener(this);
            }
        });
    }
    dialog.setVisible(true);
    dialog.dispose();
    pane.setMessage(null);

    Object selectedValue = pane.getValue();
    if (selectedValue != null) {
        if (options == null) {
            if (selectedValue instanceof Integer) {
                return ((Integer) selectedValue).intValue();
            }
        } else {
            int length = options.length;
            for (int i = 0; i < length; i++) {
                if (options[i].equals(selectedValue)) {
                    return i;
                }
            }
        }
    }
    return JOptionPane.CLOSED_OPTION;
}