-4

I would like to use button1.setText("test"); multiple times without repeating the .setText("test"); for every button. something like this button1, button2, button3.setText("test");

Is this a normal question or am i just lazy.

(sorry for my english)

0

3 Answers 3

1

You may put the buttons in an array for example, iterate on them and call the method :

JButton[] buttons = {button1, button2, button3};
for (JButton b : buttons) b.setText("test");
Sign up to request clarification or add additional context in comments.

Comments

1

You can use an array of buttons

for (JButton b : buttons) b.setText("test");

Comments

0

Yes, you are lazy.

If you are using Java 8:

Arrays.asList(button1, button2, button3).forEach(button -> button.setText("test"));

But I would prefer:

button1.setText("test");
button2.setText("test");
button3.setText("test");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.