0

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

    }

}
Reshad
  • 2,570
  • 8
  • 45
  • 86
  • 1. _Is it possible this way or do I have to use something else?_ Hard to say when you don't provide the relevant code (creation of the JButton and action/ActionListener) 2. _if (value1==false)_ the short and more readable version is `if(!value1)` 3. To toggle a value just write `value1 = !value1;` 4. You redeclare a local variable `value1` in your action method!!! This is just looking for trouble – Guillaume Polet Oct 03 '12 at 12:56
  • @GuillaumePolet i posted the actionlistener and now if i use the button it changes from false to true but if i click it again it won't change back.. – Reshad Oct 03 '12 at 13:14
  • 1
    Remove the wrapping `if(!aan)` – Guillaume Polet Oct 03 '12 at 13:55
  • i modified it again but i still get a one way change. once its true it won't get false again. – Reshad Oct 03 '12 at 13:56
  • sorry i understood it wrong! it works now thanks alot! – Reshad Oct 03 '12 at 14:00

4 Answers4

2

Not sure exactly what you're asking but to toggle the value of a boolean you can use:

value1 = !value1;

or

value1 ^= true;

Then print your variable:

System.out.println(value1);
aws
  • 136
  • 5
  • hmm thanks for that! didn;t know that.. but the problem is that my jButton doesn't work. if( e.getSource() == toggle1 ) { if i click the button nothing happens. – Reshad Oct 03 '12 at 12:48
2

As suggested in How to Use Buttons, JToggleButton may be a good choice for this, as the isSelected() predicate reflects the button's state.

state = button.isSelected()

Examples may be found here, here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

just do this?:

value1 != value1;

this inverst the current value: so if false, it will change to true, and vice versa.

EDIT: Should be:

value = !value;
Nicholas
  • 5,430
  • 3
  • 21
  • 21
  • but i this is also a way without the use of a button. unfortunately i have to use a button to toggle this on or off – Reshad Oct 03 '12 at 13:00
  • this would perfectly work with a button? just throw it in you actionlistener (or how it is called, because it has been a while that I've programmed java :) ) – Nicholas Oct 03 '12 at 13:03
0

If you want to toggle a boolean variable when pressing a button, you should use a JCheckBox instead of JButton for that purpose which has an internal boolean state and it updates this variable on its own. The check box also makes this boolean state visible to the user.

When you need the boolean variable, you can ask it with the JCheckBox.isSelected() method.

icza
  • 389,944
  • 63
  • 907
  • 827
  • i am doing an exercise where i have to use a button. a checkbox is not an option here :) – Reshad Oct 03 '12 at 12:51
  • @Reshad A checkbox is a `JToggleButton` which is in turn an `AbstractButton` ... so it is a button – Robin Oct 03 '12 at 13:01