Skip to content

Instantly share code, notes, and snippets.

@mhilbrunner
Created January 22, 2016 04:19
Show Gist options
  • Save mhilbrunner/3f25ae918294480e249c to your computer and use it in GitHub Desktop.
Save mhilbrunner/3f25ae918294480e249c to your computer and use it in GitHub Desktop.
Java/libGDX barebones dialog text input and selection with simple validation, hacked together
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class GUI {
public static final String DEFAULT_CHOOSE_TITLE = "Selection";
public static final String DEFAULT_CHOOSE_MSG = "Please choose:";
public static final String DEFAULT_INPUT_TITLE = "Input";
public static final int PLAIN_MESSAGE = JOptionPane.PLAIN_MESSAGE;
public static final int ERROR_MESSAGE = JOptionPane.ERROR_MESSAGE;
public static final int INFO_MESSAGE = JOptionPane.INFORMATION_MESSAGE;
public static final int WARNING_MESSAGE = JOptionPane.WARNING_MESSAGE;
public static final int QUESTION_MESSAGE = JOptionPane.QUESTION_MESSAGE;
public static final String CHARS_UPPERCASE =
"ABCDEFGHIJKLMNOPQRSTUVWXYZÄÜÖ";
public static final String CHARS_LOWERCASE =
"abcdefghijklmnopqrstuvwxyzäüö";
public static final String CHARS_ALPHABET =
CHARS_UPPERCASE + CHARS_LOWERCASE;
public static final String CHARS_NUM =
"0123456789";
public static final String CHARS_ALPHANUMERIC =
CHARS_ALPHABET + CHARS_NUM;
public static final String CHARS_SPECIAL =
"!§$%&/()=?+-_.,;><|^°'#{[]}´`²³\\";
public static final String CHARS_ALPHA_SPECIAL =
CHARS_ALPHANUMERIC + CHARS_SPECIAL;
public GUI() {
switchToNativeLookAndFeel();
}
public void switchToNativeLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println("Error switching to native look and feel", e);
}
}
public <T extends Object> T chooseUsingSystemPopup(final T[] choices) {
return chooseUsingSystemPopup(
DEFAULT_CHOOSE_MSG, DEFAULT_CHOOSE_TITLE,
JOptionPane.PLAIN_MESSAGE, choices, choices[0]);
}
public <T extends Object> T chooseUsingSystemPopup(
String msg, String title, final T[] choices) {
return chooseUsingSystemPopup(
msg, title, JOptionPane.PLAIN_MESSAGE, choices, choices[0]);
}
public <T extends Object> T chooseUsingSystemPopup(
String msg, String title, int messageType,
final T[] choices) {
return chooseUsingSystemPopup(msg, title, messageType, choices, choices[0]);
}
@SuppressWarnings("unchecked")
public <T extends Object> T chooseUsingSystemPopup(
String msg, String title, int messageType,
final T[] choices,
T initialChoice) {
return (T) JOptionPane.showInputDialog(null, msg, title, messageType,
null, choices, initialChoice);
}
public String getStringUsingSystemPopup(String msg) {
return getStringUsingSystemPopup(msg, DEFAULT_INPUT_TITLE, 0);
}
public String getStringUsingSystemPopup(String msg, String title) {
return getStringUsingSystemPopup(msg, title, 0);
}
public String getStringUsingSystemPopup(String msg,
String title,
int messageType) {
return JOptionPane.showInputDialog(null, msg, title, messageType);
}
public String getStringUsingSystemPopup(String msg,
String title,
int messageType,
int minLength,
int maxLength,
String allowedChars,
boolean cancelEnabled) {
String val;
while (true) {
val = getStringUsingSystemPopup(msg, title, messageType);
if (val == null) {
if (cancelEnabled) {
return null;
}
continue;
}
if (val.length() < minLength) {
continue;
}
if (val.length() > maxLength) {
continue;
}
if (allowedChars != null && allowedChars.length() > 0) {
for (char aChar : val.toCharArray()) {
if (allowedChars.indexOf(aChar) == -1) {
continue;
}
}
}
return val;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment