Java examples for Swing:JOptionPane
Shows an editable option dialog box.
import javax.swing.*; import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkEvent.EventType; import javax.swing.event.HyperlinkListener; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.URL; import java.util.Arrays; public class Main{ /**//w w w . ja va 2 s . c o m * Shows an editable option dialog box. * The window title is "Input". * * @param prompt the string to prompt the user with * @param possibleValues array of options to display * @return the toString()ed item the user selected, or whatever was typed in */ public static String showOptionInputDialog(final String prompt, final Object[] possibleValues) { return showOptionInputDialog("Input", prompt, possibleValues, possibleValues[0]); } /** * Shows an editable option dialog box. * The window title can be specified. * * @param title window title * @param prompt the string to prompt the user with * @param possibleValues array of options to display * @return the toString()ed item the user selected, or whatever was typed in */ public static String showOptionInputDialog(final String title, final String prompt, final Object[] possibleValues) { return showOptionInputDialog(title, prompt, possibleValues, possibleValues[0]); } /** * Shows an editable option dialog box. * The window title can be specified. * * @param title window title * @param prompt the string to prompt the user with * @param possibleValues array of options to display * @param defaultValue the initial contents of the combobox * @return the toString()ed item the user selected, or whatever was typed in */ public static String showOptionInputDialog(final String title, final String prompt, final Object[] possibleValues, final Object defaultValue) { return showOptionInputDialog(parent, title, prompt, possibleValues, defaultValue); } /** * Shows an editable option dialog box. * The window title can be specified. * * @param parent the parent for the dialog * @param title window title * @param prompt the string to prompt the user with * @param possibleValues array of options to display * @param defaultValue the initial contents of the combobox * @return the toString()ed item the user selected, or whatever was typed in */ public static String showOptionInputDialog(final Component parent, final String title, final String prompt, final Object[] possibleValues, final Object defaultValue) { final JComboBox box = new JComboBox(possibleValues); box.setEditable(true); box.setSelectedItem(defaultValue); JOptionPane pane = new JOptionPane(new Object[] { prompt, box }, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION); pane.setWantsInput(false); JDialog dialog = pane.createDialog(parent, title); dialog.pack(); dialog.setVisible(true); Integer value = (Integer) pane.getValue(); if (value == null || value.intValue() == JOptionPane.CANCEL_OPTION || value.intValue() == JOptionPane.CLOSED_OPTION) { return null; } return box.getSelectedItem().toString(); } }