Hier die vier Typen:
- showConfirmDialog
Fragt beim Benutzer nach, der Ja, Nein oder Abbruch klicken kann.
- showInputDialog
Fragt nach einer Eingabe.
- showMessageDialog
Gibt einen Hinweis aus.
Hier einige Beispiele für die Benutzung.
// Zeigt eine Fehlermeldung an.
JOptionPane.showMessageDialog(null, "Fehler", "Fehler", JOptionPane.ERROR_MESSAGE);
// Zeigt einen JInternalFrame mit einer Information an.
JOptionPane.showInternalMessageDialog(frame, "Information", "Information", JOptionPane.INFORMATION_MESSAGE);
// Gibt die Auswahl zwischen Ja und Nein vor
JOptionPane.showConfirmDialog(null, "Sicher?", "Sicher?", JOptionPane.YES_NO_OPTION);
// Zeigt ein JInternalFrame an mit der Wahl zwischen Ja, Nein und Abbruch.
JOptionPane.showInternalConfirmDialog(frame, "Wollen Sie das Dokument vor dem Beenden speichern?", "Speichern", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
// Zeigt einen Warndialog mit »OK« und »CANCEL« an. »OK« ist dabei als Standard-Button gesetzt.
Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Warnung", "Warnung", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
// Fragt den Benutzer nach einem String.
String inputValue = JOptionPane.showInputDialog("Bitte einen Wert eingeben");
// Bittet den Benutzer, einen String auszuwählen
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null, "Bitte wählen Sie aus", "Eingabe", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
Zusätzlich kann man auch selbst Exemplare etzeugen. Das folgende Beispiel zeigt den grundsätzlichen Aufbau:
JOptionPane pane = new JOptionPane(...);
pane.setXXX(...); // Konfigurieren
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.show();
Object selectedValue = pane.getValue();
if(selectedValue == null)
return CLOSED_OPTION;
// Wenn es keine Optionen gibt
if(options == null) {
if(selectedValue instanceof Integer)
return ((Integer)selectedValue).intValue();
return CLOSED_OPTION;
}
// Wenn es Optionen gibt
for(int counter = 0, maxCounter = options.length;
counter < maxCounter; counter++) {
if(options[counter].equals(selectedValue))
return counter;
}
return CLOSED_OPTION;
| Methoden |
| public static String | showInputDialog(Object message) |
| public static String | showInputDialog(Object message, Object initialSelectionValue) |
| public static String | showInputDialog(Component parentComponent, Object message) |
| public static String | showInputDialog(Component parentComponent, Object message, Object initialSelectionValue) |
| public static String | showInputDialog(Component parentComponent, Object message, String title, int messageType) |
| public static Object | showInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) |
| public static void | showMessageDialog(Component parentComponent, Object message) |
| public static void | showMessageDialog(Component parentComponent, Object message, String title, int messageType) |
| public static void | showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon) |
| public static int | showConfirmDialog(Component parentComponent, Object message) |
| public static int | showConfirmDialog(Component parentComponent, Object message, String title, int optionType) |
| public static int | showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType) |
| public static int | showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon) |
| public static int | showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) |
| public JDialog | createDialog(Component parentComponent, String title) |
| public static void | showInternalMessageDialog(Component parentComponent, Object message) |
| public static void | showInternalMessageDialog(Component parentComponent, Object message, String title, int messageType) |
| public static void | showInternalMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon) |
| public static int | showInternalConfirmDialog(Component parentComponent, Object message) |
| public static int | showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType) |
| public static int | showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType) |
| public static int | showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon) |
| public static int | showInternalOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) |
| public static String | showInternalInputDialog(Component parentComponent, Object message) |
| public static String | showInternalInputDialog(Component parentComponent, Object message, String title, int messageType) |
| public static Object | showInternalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) |
| public JInternalFrame | createInternalFrame(Component parentComponent, String title) |
| public static Frame | getFrameForComponent(Component parentComponent) |
| public static JDesktopPane | getDesktopPaneForComponent(Component parentComponent) |
| public static void | setRootFrame(Frame newRootFrame) |
| public static Frame | getRootFrame() |
| public void | setUI(OptionPaneUI ui) |
| public OptionPaneUI | getUI() |
| public void | updateUI() |
| public String | getUIClassID() |
| public void | setMessage(Object newMessage) |
| public Object | getMessage() |
| public void | setIcon(Icon newIcon) |
| public Icon | getIcon() |
| public void | setValue(Object newValue) |
| public Object | getValue() |
| public void | setOptions(Object[] newOptions) |
| public Object[] | getOptions() |
| public void | setInitialValue(Object newInitialValue) |
| public Object | getInitialValue() |
| public void | setMessageType(int newType) |
| public int | getMessageType() |
| public void | setOptionType(int newType) |
| public int | getOptionType() |
| public void | setSelectionValues(Object[] newValues) |
| public Object[] | getSelectionValues() |
| public void | setInitialSelectionValue(Object newValue) |
| public Object | getInitialSelectionValue() |
| public void | setInputValue(Object newValue) |
| public Object | getInputValue() |
| public int | getMaxCharactersPerLineCount() |
| public void | setWantsInput(boolean newValue) |
| public boolean | getWantsInput() |
| public void | selectInitialValue() |
| protected String | paramString() |
| public AccessibleContext | getAccessibleContext() |