-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathJEnhancedOptionPane.java
More file actions
40 lines (34 loc) · 1.66 KB
/
JEnhancedOptionPane.java
File metadata and controls
40 lines (34 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package io.github.arrayv.panes;
import javax.swing.*;
import java.awt.*;
// Many thanks to Freek de Bruijn on StackOverflow for providing a custom JOptionPane.
// https://stackoverflow.com/questions/14407804/how-to-change-the-default-text-of-buttons-in-joptionpane-showinputdialog?noredirect=1&lq=1
public final class JEnhancedOptionPane extends JOptionPane {
private static final long serialVersionUID = 1L;
/**
* Prompts the user with a textbox and returns their answer, or null if they didn't confirm.
* The buttons are customizable, but option 0 is always defined as the Confirm action.
*
* @param title Title bar
* @param message Prompt message
* @param options Buttons to be clicked. You usually just want to pass a String[] of labels here, e.g. ["OK", "Cancel"].
*
* @return The user input, or null if they closed out or picked a secondary option.
*/
public static String showInputDialog(final String title, final Object message, final Object[] options)
throws HeadlessException {
final JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE,
OK_CANCEL_OPTION, null,
options, null);
pane.setWantsInput(true);
pane.setComponentOrientation((getRootFrame()).getComponentOrientation());
pane.setMessageType(QUESTION_MESSAGE);
pane.selectInitialValue();
final JDialog dialog = pane.createDialog(null, title);
dialog.setVisible(true);
dialog.dispose();
final Object input = pane.getInputValue();
final Object button = pane.getValue();
return button == options[0] ? (String) input : null;
}
}