Escape and cancel on user input dialog
in
Programming Questions
•
2 years ago
I managed to get the code not to crash with any key press except for cancel escape and the small cross on the input dialog.
Will anybody please help on that? Is there a unicode character corresponding to these keys so I can filter it or can I disable them or is there a way to display a userinput dialog without a cancel button?
Thanks
Bertus
import javax.swing.JOptionPane;//For user input dialogs
boolean isInteger;
String y = "0";
boolean isInteger;
String y = "0";
class Button {
int x, y; // The x- and y-coordinates
int size; // Dimension (width and height)
color baseGray; // Default gray value
color overGray; // Value when mouse is over the button
color pressGray; // Value when mouse is over and pressed
boolean over = false; // True when the mouse is over
boolean pressed = false; // True when the mouse is over and pressed
Button(int xp, int yp, int s, color b, color o, color p) {
x = xp;
y = yp;
size = s;
baseGray = b;
overGray = o;
pressGray = p;
}
// Updates the over field every frame
void update() {
if ((mouseX >= x) && (mouseX <= x+size) &&
(mouseY >= y) && (mouseY <= y+size)) {
over = true;
} else {
over = false;
}
}
boolean press() {
if (over == true) {
pressed = true;
return true;
} else {
return false;
}
}
void release() {
pressed = false; // Set to false when the mouse is released
}
void display() {
if (pressed == true) {
fill(pressGray);
} else if (over == true) {
fill(overGray);
} else {
fill(baseGray);
}
stroke(255);
rect(x, y, size, size);
}}
Button button1, button2, button3;
int mode = 0;
void setup() {
frameRate (2);
size(100, 100);
smooth();
color gray = color(204);
color white = color(255);
color black = color(0);
button1 = new Button(10, 80, 10, gray, white, black);
}
void draw(){
frameRate (2);
background(204);
manageButtons();
noStroke();
fill(0);
isInteger = true;
int x, y; // The x- and y-coordinates
int size; // Dimension (width and height)
color baseGray; // Default gray value
color overGray; // Value when mouse is over the button
color pressGray; // Value when mouse is over and pressed
boolean over = false; // True when the mouse is over
boolean pressed = false; // True when the mouse is over and pressed
Button(int xp, int yp, int s, color b, color o, color p) {
x = xp;
y = yp;
size = s;
baseGray = b;
overGray = o;
pressGray = p;
}
// Updates the over field every frame
void update() {
if ((mouseX >= x) && (mouseX <= x+size) &&
(mouseY >= y) && (mouseY <= y+size)) {
over = true;
} else {
over = false;
}
}
boolean press() {
if (over == true) {
pressed = true;
return true;
} else {
return false;
}
}
void release() {
pressed = false; // Set to false when the mouse is released
}
void display() {
if (pressed == true) {
fill(pressGray);
} else if (over == true) {
fill(overGray);
} else {
fill(baseGray);
}
stroke(255);
rect(x, y, size, size);
}}
Button button1, button2, button3;
int mode = 0;
void setup() {
frameRate (2);
size(100, 100);
smooth();
color gray = color(204);
color white = color(255);
color black = color(0);
button1 = new Button(10, 80, 10, gray, white, black);
}
void draw(){
frameRate (2);
background(204);
manageButtons();
noStroke();
fill(0);
isInteger = true;
int y1 = 100;
if (mode == 1) {
y = JOptionPane.showInputDialog(null,
"Enter new setpoint (0 to 120):",
"Setpoint adjustment",
JOptionPane.PLAIN_MESSAGE);}
if (y == null) {
isInteger = false;
} int length = y.length();
if (length == 0) {
isInteger = false;
}
int i = 0;
if (length == 1) {
if (y.charAt(0) == '-') {
isInteger = false;
}
i = 0;
}
for (; i < length; i++) {
char c = y.charAt(i);
if (c <= '/' || c >= ':') {
isInteger = false;
}
}
mode = 0;
if(isInteger == true){
y1 = Integer.parseInt(y);}
else{println(" Not integer");}
println(y1);
}
void manageButtons() {
button1.update();
button1.display();
}
void mousePressed() {
if (button1.press() == true) { mode = 1; }
}
void mouseReleased() {
button1.release();
}
if (mode == 1) {
y = JOptionPane.showInputDialog(null,
"Enter new setpoint (0 to 120):",
"Setpoint adjustment",
JOptionPane.PLAIN_MESSAGE);}
if (y == null) {
isInteger = false;
} int length = y.length();
if (length == 0) {
isInteger = false;
}
int i = 0;
if (length == 1) {
if (y.charAt(0) == '-') {
isInteger = false;
}
i = 0;
}
for (; i < length; i++) {
char c = y.charAt(i);
if (c <= '/' || c >= ':') {
isInteger = false;
}
}
mode = 0;
if(isInteger == true){
y1 = Integer.parseInt(y);}
else{println(" Not integer");}
println(y1);
}
void manageButtons() {
button1.update();
button1.display();
}
void mousePressed() {
if (button1.press() == true) { mode = 1; }
}
void mouseReleased() {
button1.release();
}
1