0

So I have 44 buttons in my program, and for one of my methods I want to re-enable all of them. the buttons are easily named btn1, btn2, btn3...btn44. Is there a way I can use a for loop to enable all of them?

I would love to do something like this but I cannot find the resources necessary.

for(int i == 0, i < 44, i++){
  btn<i>.setEnabled(true);
}

Without that I would have to go through each button

btn1.setEnabled(true);
btn2.setEnabled(true);
...
btn44.setEnabled(true);

I know this alternate method isn't that bad but I have similar areas in my code where a technique like the one I am looking for would be very useful. Thank you!

5
  • 6
    why not store all of them into an array or a list? Commented Jan 25, 2018 at 20:09
  • do you have all buttons in a List or similar? If not, how do you keep track of them? Commented Jan 25, 2018 at 20:09
  • 1
    Instead of saving each button individually you should manage them in a Collection like a List, ArrayList for example. Then you can just call for (Button b : buttons) { button.setEnabled(true); } if buttons is the said list. Commented Jan 25, 2018 at 20:09
  • @ochi I think OP has 44 individual variables. Good that he can now remove the clutter using arrays and lists. Commented Jan 25, 2018 at 20:14
  • @Zabuza that's a big assumption you are making Commented Jan 25, 2018 at 20:15

3 Answers 3

3

You should make an array of buttons:

Button[] buttons = new Button[44];
for (int i = 0; i < 44; i++) {
    // Do stuff with buttons[i]
}

You can't get the value of a variable using its string name representation because that would not compile very well; it is possible but not just using Java and it would require some weird roundabout way of doing it. Just use an array.

Sign up to request clarification or add additional context in comments.

6 Comments

or some collection like List (ArrayList for example). Note that enhanced for-loop might be more convenient here: for (Button b : buttons) { ... } But I would recommend to show both based on the questions level.
@Zabuza That works as well, though I'd only recommend that for when the number of buttons is unknown and can change during runtime.
@HyperNeutrino List is perfect for this scenario, ArrayList uses an array underneath and the use of List will enable more flexibility if required in the future. Arrays are still valuable for manipulating large amounts of items, for instance the bytes of a file, or a stream from the network; 44 elements should by no means represent a efficiency concern.
if not all the elements in the array are initialized, this approach will be problematic - or if there is a need to have more than 44 buttons, or ....
@ochi - more that 44 buttons is not problematic, just use that number; problematic is if the number of buttons is going to change at runtime, then you'd be much better off with a Collection. In any case, if you are using an array, don't use the magic number 44 - define it such as private static final int BUTTON_COUNT = 44; so you change it in one place if the number ever changes. I personally would go with @OscarRyz ArrayList - more flexible, has good random access performance for buttons.get(17)
|
2

Create a list to store all the buttons and the iterate it.

...
List<Button> buttons = new ArrayList<>();
buttons.add(btn1);
buttons.add(btn2);
...
buttons.add(btn42);

And then use that list for mass actions:

void setStatus(boolean enabled) {
   for (Button b : buttons ) {
      b.setEnabled(enabled);
   }
}

Comments

0

You could add you Buttons to a collection, like a List (if you have not done so already). Then it's easier to iterate over them.

    // this list will grow automatically when you add new elements
    List<Button> buttons = new ArrayList<>();

    // when you create a button in your code, add them to your collection/list
    buttons.add(new Button("1"));
    buttons.add(new Button("2"));
    buttons.add(new Button("3"));
    buttons.add(new Button("4"));
    buttons.add(new Button("5"));
    // etc.

    // in Java 8 you can use lambdas to update your buttons like this
    buttons.forEach(button -> button.setEnabled(true));

Just make sure you are importing the correct version of List.

I.e make sure you use this list import java.util.List; and not the one in the windowing toolkit (i.e. not import java.awt.List;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.