Reputation: 14179
I had created a JOptionPane
of type showInputDialog
. When it opens it, it shows me two buttons: OK and Cancel. I would like to handle the action when I push on Cancel button, but I don't know how to reach it. How can I get it?
Upvotes: 14
Views: 85790
Reputation: 1
This may be your answer:
package Joptionpane;
import javax.swing.JOptionPane;
public class Cancle_on_JOptionPane {
public static void main(String[] args) {
String s;
int i;
// Set the process limit to any number, that use when you need to use for comparability and limited to get out of the program when the answer is correct.(in this case is 100)
for(i=0;i<100;i++){
s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?");
// Resolve process by compare answer with character to got the right answer, If it's no character it would show question box until user type answer or click cancel.
try{
if(s.equals("")) {
JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=2; // Set i=2 to repeating question box loop until user give answer.
}
else {
JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100; // Get out from question box when the variable is not empty.
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100; // Get out from question box when user click cancel and showing "cancel message", You can link to another action here.
}
}
}
}
Upvotes: -2
Reputation: 6258
showInputDialog
return NULL
if you click cancel and String object even if it's empty.
So all you need to do is test if it's Null and if it's not empty.
Upvotes: 0
Reputation: 91
This is an old question, and I am an Java newbie so there might be better solutions, but I wanted to know the same, and maybe it can help others, what I did was to check if the response was null.
If the user clicks "cancel", the response will be null. If they click "ok" without entering any text the response will be the empty string.
This worked for me:
//inputdialog
JOptionPane inpOption = new JOptionPane();
//Shows a inputdialog
String strDialogResponse = inpOption.showInputDialog("Enter a number: ");
//if OK is pushed then (if not strDialogResponse is null)
if (strDialogResponse != null){
(Code to do something if the user push OK)
}
//If cancel button is pressed
else{
(Code to do something if the user push Cancel)
}
Upvotes: 9
Reputation: 1
You could do it like this:
String val = JOptionPane.showInputDialog("Value: ");
if(val == null){
// nothing goes here if yo don't want any action when canceled, or
// redirect it to a cancel page if needed
}else{
//insert your code if ok pressed
// JOptionPane return an String, as you was talking about int
int value = Integer.ParseInt(val);
}
Upvotes: 0
Reputation: 1
package Joptionpane;
import javax.swing.JOptionPane;
public class Cancle_on_JOptionPane {
public static void main(String[] args) {
String s;
int i;
for (i=0;i<100;i++){
s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?");
try {
if (s.equals("")) {
JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=2;
} else {
JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100;
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100;
}
}
}
}
Upvotes: -1
Reputation: 21223
For example:
int n = JOptionPane.showConfirmDialog(
frame, "Would you like green eggs and ham?",
"An Inane Question",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
} else if (n == JOptionPane.NO_OPTION) {
} else {
}
Alternatively with showOptionDialog
:
Object[] options = {"Yes, please", "No way!"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like green eggs and ham?",
"A Silly Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (n == JOptionPane.YES_OPTION) {
} else if (n == JOptionPane.NO_OPTION) {
} else {
}
See How to Make Dialogs for more details.
EDIT: showInputDialog
String response = JOptionPane.showInputDialog(owner, "Input:", "");
if ((response != null) && (response.length() > 0)) {
}
Upvotes: 26
Reputation: 285405
The showMessageDialog, shouldn't show two buttons, so something is amiss with either your code or your interpretation of it. Regardless, if you want to give the user an choice and want to detect that choice, don't use a showMessageDialog but rather a showConfirmDialog, and get the int returned and test it to see if it is JOptoinPane.OK_OPTION.
Upvotes: 6