I have a JOptionPane I've been working with this morning as I'm trying to learn Swing! I've been looking at the reference pages by oracle, and the farthest I got was to make 2 buttons that when you click either of them, they close (so they do the same thing.) Is there a way I can check when a button is clicked and do something (like printing something?)
My code works, I just need help on what to do next.
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
JOptionPane frame = new JOptionPane();
//Custom button text
Object[] options = {"Barack",
"Obama"};
int n = JOptionPane.showOptionDialog(frame,
"His name is..",
"Obamas name is?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //do not use a custom Icon
options, //the titles of buttons
options[0]); //default button title
}
}
Output when running:
CodePudding user response:
You can check if a JOptionPane button has been pressed by using things like YES_OPTION and NO_OPTION.
if(n == OptionPane.YES_OPTION){
//do this
}
This works the same for NO_OPTION and also OK_OPTION. Hopefully this helps :)