Skip to content

Instantly share code, notes, and snippets.

@codemee
Forked from GoToLoop/Inputs.java
Last active July 31, 2020 02:38
Show Gist options
  • Save codemee/1a483548c9a6a2454c53da994f9e71ff to your computer and use it in GitHub Desktop.
Save codemee/1a483548c9a6a2454c53da994f9e71ff to your computer and use it in GitHub Desktop.
/**
* Keyboard Input Library
* Andrew Errity v0.2 (2015-Oct-01)
* GoToLoop v1.0.4 (2015-Oct-22)
*
* https://Forum.Processing.org/two/discussion/13175/
* do-whille-is-not-working-how-it-suppose-to#Item_12
*
* https://GitHub.com/aerrity/Inputs/blob/master/src/Inputs.java
* https://Gist.GitHub.com/GoToLoop/bba0c288aaeeb5ef9bb1
*/
package iadt.creative;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.*;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
public final class Inputs {
protected static final String TITLE = "Input required!", CHOOSE = "Choose one:";
protected static final int CHARS = 25;
protected static final JTextField field = new JTextField(CHARS);
protected static final JLabel label = new JLabel();
protected static final JPanel panel = new JPanel(new BorderLayout(5, 2));
static {
panel.add(label, BorderLayout.WEST);
panel.add(field);
}
protected static final JDialog dialog = new JOptionPane(panel, QUESTION_MESSAGE) {
@Override public final void selectInitialValue() {
field.requestFocusInWindow();
}
}
.createDialog(null, TITLE);
public static final String readString(final String txt) {
return readString(txt, "");
}
public static final String readString(final String txt, final String defaultString) {
label.setText(txt);
field.setText(defaultString);
dialog.setVisible(true);
return field.getText();
}
public static final boolean readBoolean(final String label) {
return showConfirmDialog(null, label, CHOOSE, YES_NO_OPTION) == YES_OPTION;
}
public static final byte readByte(final String label) {
return readByte(label, Byte.MIN_VALUE);
}
public static final byte readByte(final String label, final int failValue) {
try {
return Byte.parseByte(readString(label, Integer.toString(failValue)));
}
catch (final NumberFormatException ex) {
return (byte) failValue;
}
}
public static final short readShort(final String label) {
return readShort(label, Short.MIN_VALUE);
}
public static final short readShort(final String label, final int failValue) {
try {
return Short.parseShort(readString(label, Integer.toString(failValue)));
}
catch (final NumberFormatException ex) {
return (short) failValue;
}
}
public static final int readInt(final String label) {
return readInt(label, Integer.MIN_VALUE);
}
public static final int readInt(final String label, final int failValue) {
try {
return Integer.parseInt(readString(label, Integer.toString(failValue)));
}
catch (final NumberFormatException ex) {
return failValue;
}
}
public static final long readLong(final String label) {
return readLong(label, Long.MIN_VALUE);
}
public static final long readLong(final String label, final long failValue) {
try {
return Long.parseLong(readString(label, Long.toString(failValue)));
}
catch (final NumberFormatException ex) {
return failValue;
}
}
public static final float readFloat(final String label) {
return readFloat(label, Float.MIN_VALUE);
}
public static final float readFloat(final String label, final float failValue) {
try {
return Float.parseFloat(readString(label, Float.toString(failValue)));
}
catch (final NumberFormatException ex) {
return failValue;
}
}
public static final double readDouble(final String label) {
return readDouble(label, Double.MIN_VALUE);
}
public static final double readDouble(final String label, final double failValue) {
try {
return Double.parseDouble(readString(label, Double.toString(failValue)));
}
catch (final NumberFormatException ex) {
return failValue;
}
}
}
/**
* Keyboard Input Library
* Andrew Errity v0.2 (2015-Oct-01)
* GoToLoop v1.0.4 (2015-Oct-22)
*
* https://Forum.Processing.org/two/discussion/13175/
* do-whille-is-not-working-how-it-suppose-to#Item_12
*
* https://GitHub.com/aerrity/Inputs/blob/master/src/Inputs.java
* https://Gist.GitHub.com/GoToLoop/bba0c288aaeeb5ef9bb1
*/
import static iadt.creative.Inputs.*;
void setup() {
getSurface().setVisible(false);
byte age;
do {
while ((age = readByte("Enter your age:")) < 0);
println("Your age is:", age);
} while (readBoolean("Again?"));
exit();
}
@codemee
Copy link
Author

codemee commented Jul 31, 2020

Display fail value so that user can see the default value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment