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

The following examples show how to use javax.swing.JOptionPane#selectInitialValue() . 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: JOptionInput.java    From jeveassets with GNU General Public License v2.0 6 votes vote down vote up
private static Object internalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) {
	JOptionPane pane = new JOptionPane(message, messageType,
			OK_CANCEL_OPTION, icon,
			null, null);

	pane.setWantsInput(true);
	pane.setSelectionValues(selectionValues);
	pane.setInitialSelectionValue(initialSelectionValue);
	pane.setComponentOrientation(((parentComponent == null)
			? getRootFrame() : parentComponent).getComponentOrientation());

	TextManager.installAll(pane);

	JDialog dialog = pane.createDialog(parentComponent, title);

	pane.selectInitialValue();
	dialog.setVisible(true);
	dialog.dispose();

	Object value = pane.getInputValue();

	if (value == UNINITIALIZED_VALUE) {
		return null;
	}
	return value;
}
 
Example 3
Source File: Messages.java    From jclic with GNU General Public License v2.0 5 votes vote down vote up
private static boolean showDlg(Component parent, JOptionPane pane, String title) {
  if (parent != null && !(parent instanceof java.awt.Frame)) {
    parent = JOptionPane.getFrameForComponent(parent);
  }
  JDialog dialog = pane.createDialog(parent, title);
  pane.selectInitialValue();
  return showDlg(dialog);
}
 
Example 4
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;
}
 
Example 5
Source File: Desktop.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
void doConnect()
{
  final String[] options = new String[Activator.remoteHosts.size()];
  Activator.remoteHosts.copyInto(options);

  // The selection comp I want in the dialog
  final JComboBox combo = new JComboBox(options);
  combo.setEditable(true);

  // Mindboggling complicate way of creating an option dialog
  // without the auto-generated input field

  final JLabel msg = new JLabel(Strings.get("remote_connect_msg"));
  final JPanel panel = new JPanel(new BorderLayout());

  panel.add(combo, BorderLayout.SOUTH);
  panel.add(msg, BorderLayout.NORTH);

  final JOptionPane optionPane =
    new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE);
  optionPane.setIcon(connectIconLarge);
  optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
  optionPane.setWantsInput(false);
  optionPane.setOptions(new String[] { Strings.get("ok"),
                                      Strings.get("cancel"),
                                      Strings.get("local"), });

  optionPane.selectInitialValue();

  final JDialog dialog =
    optionPane.createDialog(frame, Strings.get("remote_connect_title"));
  dialog.setVisible(true);
  dialog.dispose();

  if (!(optionPane.getValue() instanceof String)) { // We'll get an Integer if
                                                    // the user pressed Esc
    return;
  }

  final String value = (String) optionPane.getValue();

  if (Strings.get("cancel").equals(value)) {
    return;
  }

  String s = (String) combo.getSelectedItem();

  if (Strings.get("local").equals(value)) {
    s = "";
  }

  if (!Activator.remoteHosts.contains(s)) {
    Activator.remoteHosts.addElement(s);
  }

  if ((s != null)) {
    Activator.openRemote(s);
  }
}