Java code completion

I have a list of checkboxes that I would like to disable. Instead of typing

c1.setEnabled(false);
c2.setEnabled(false);
c3.setEnabled(false);
c4.setEnabled(false);
c5.setEnabled(false);

How could I trim this code by putting them in some group? I have the same problem in many of my codes, but with different components. Thanks

+4
source share
2 answers

In Java 8+ you can use lambda like

Stream.of(c1, c2, c3, c4, c5).forEach(x -> x.setEnabled(false));
+9
source

Put them in an ArrayList or other collection. Write a function that allows or does something for each element. This is very useful for user interfaces, where you need to perform various actions on several components.

+2
source

All Articles