Hello I would like to know. How can I set a variable to true or false and vice versa with a JButton? My first thought would be create the variables like
private boolean value1, value2;
and the buttons like
private JButton toggle1, toggle2;
// see code below
The problem is that it won't react on the button somehow. Is it possible this way or do I have to use something else?
edit: here is the relevant code. ( my ActionListener)
public void actionPerformed(ActionEvent e) {
if( e.getSource() == toggle1 ) {
if(aan1 == false) {
aan1 ^= true;
System.out.println(aan1);
}
else if(aan1 == true) {
aan1 ^= false;
}
}
try {
// controleer of de ingevulde waarde tussen de 0 en de 15 is
if( e.getSource() == burn && Integer.parseInt(textfield.getText()) < 16 && Integer.parseInt(textfield.getText()) > 0) {
// brand kaars
if( height > 15 && aan1 == true) {
int aantal = Integer.parseInt(textfield.getText());
height -= aantal;
}
if( height2 > 15 && aan2 == true) {
int aantal = Integer.parseInt(textfield.getText());
height2 -= aantal;
}
// opnieuw tekenen
repaint();
}
else {
JOptionPane.showMessageDialog(null, "error: vul een getal tussen de 0 en 15 in!"); // alertbox melding
}
}
catch(NumberFormatException error) {
JOptionPane.showMessageDialog(null, "error: een van de velden bevat geen cijfer of is niet ingevuld!"); // alertbox melding
}
}