JButton button = new JButton(" >> JavaProgrammingForums.com <<");
//Add action listener to button
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
a = 5 ;
System.out.println("You clicked the button");
}
});
Java: How to set value when button is clicked?
-
11-07-2023 - |
Question
I need to program a button which sets value when it's clicked. How to write an actionPerformed method to work so?
int a = 0;
void setValue(){
a = 5;
}
public void actionPerformed(ActionEvent e) {
/*when*/ e.getActionCommand() /*call setValue()*/
}
Solution
OTHER TIPS
Make a button and then add ActionListener to it like this
private static JButton btn;
private statin int a;
btn = new JButton();
int a = 0;
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
a = 15;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow