简体   繁体   中英

java text dialog, which button was clicked?

I followed official guide for java dialogs so I have this code:

String s = JOptionPane.showInputDialog(this, "Text\n\nEnter your name", "Heading", JOptionPane.PLAIN_MESSAGE);

How can I check if an user clicked OK or cancel button?

Try this:

String input = JOptionPane.showInputDialog(this, "Text\n\nEnter your name", "Heading", JOptionPane.PLAIN_MESSAGE);
if (input!=null) { ....}

Note that when the user clicks on "cancel", input will be null .

So you can try like this if you want to check if the user clicks Cancel or OK button

String input = JOptionPane.showInputDialog(this, "Text\n\nEnter your name", "Heading", JOptionPane.PLAIN_MESSAGE);
if (input != null) { 
 // functionality for the OK button click
} else {
 // functionality for the Cancel button click
}

showInputDialog :

Returns:
user's input, or null meaning the user canceled the input

If method call returns null , then the user pressed cancel . Otherwise, if he pressed ok , it will contain the user input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM