I need a button that, when pressed, enables all the other buttons, and changes the name of a label from "Off" to "On", and when pressed again, disables all the buttons and turns the switch to "off" back again, like an on/off switch. The thing is, I can "turn it" on, but I can't turn it back off.
-
3Is this a Swing GUI? Android? AWT? A web program? What? If you've tried to implement this and your code doesn't work, you might consider showing us your code attempt. – Hovercraft Full Of Eels Feb 14 '13 at 02:47
-
1I bet it's Swing, since he/she tagged the question with `JButton`. – Eng.Fouad Feb 14 '13 at 02:48
-
yes, it's swing, sorry for the delay, my internet sucks! I'll try this! thanks!! – Danilo Bambi Feb 14 '13 at 02:53
4 Answers
If Swing, then perhaps you will want to place the buttons that you wish to control into an array or an ArrayList<AbstractButton>
. That way the ActionListener of the control button can simply iterate through the array or the collection with a for loop, calling setEnabled(true)
or false on the buttons. Consider making the controlling button be a JCheckBox or a JToggleButton.
Note, if you use a JToggleButton, then add an ItemListener to it. If you do so, there's no need to use booleans. Just check the state of the ItemEvent passed into the ItemListener's itemStateChanged method. If the getStateChanged()
returns ItemEvent.SELECTED, then iterate through your JButton collection enabling all buttons. If it returns ItemEvent.DESELECTED, do the opposite.
Also note as per Byron Hawkins's comment:
You may want to consider that the
ItemListener
will receive events when the button is programmatically toggled, and also when the user toggles the button. TheActionListener
only gets fired on input from the human user. I've often had bugs because I picked the wrong one.

- 283,665
- 25
- 256
- 373
-
Also consider a `ButtonGroup` of `JToggelButton`, seen [here](http://stackoverflow.com/a/6036048/230513). – trashgod Feb 14 '13 at 11:16
-
1You may want to consider that the `ItemListener` will receive events when the button is _programmatically_ toggled, and also when the _user_ toggles the button. The `ActionListener` only gets fired on input from the human user. I've often had bugs because I picked the wrong one. – Byron Hawkins Feb 14 '13 at 21:11
-
@ByronHawkins: a very good point, thanks! If you don't mind, I will add this to the answer. – Hovercraft Full Of Eels Feb 14 '13 at 21:35
-
thanks guys! with the ItemListener it worked perfect... problem solved! thank you so much! – Danilo Bambi Feb 15 '13 at 00:33
you'll need a boolean to represent the state of the button.
In other words, when your button is off (your boolean variable is false), from your onClick listener, you'll call a method "turnButtonOn()" or something of that nature.
If your boolean variable is true, then you'll call a method turnButtonOff()
public void onClick() {
if(buttonOn){
turnOff();
}
else {
turnOn();
}
buttonOn = !buttonOn;
}

- 103
- 2
- 9
If your button is pressed down and won't pop back up, chances are you have overridden a method in JToggleButton
without calling super
's version of it. Instead of overriding methods, create an ActionListener
and use addActionListener()
to attach to the button. When your listener is notified of button actions, check whether the toggle button is up or down and setEnabled()
on the other buttons accordingly.

- 2,536
- 2
- 24
- 34
try use this simple code, use the variable as the flag
public int status = 0; //0 = on, 1=off
public void button_clicked()
{
//on button clicked
if(status == 0)
{
//logic here
status = 1;
buttonname.setText("Off");
}
//off button clicked
else if(status == 1)
{
//logic here
status = 0;
buttonname.setText("On");
}
}

- 266
- 3
- 11
-
If you only have two states, why not use a boolean? It would make code if(on){...}else{...} – n00begon Feb 14 '13 at 02:57
-
yes you can. That's just my style of code. you can modify it anyway you want :) – goravine Feb 14 '13 at 02:59