Cookie
Cookie

Reputation: 9

Adding JButtons and such in bulk

I was wondering if there was a better way to add JButton to JPanel or Container etc. for example, instead of

p1.add(b1);
p1.add(b2);
p1.add(b3);

Could you do this, or something like that?

p1.add(b1,b2,b3);

Upvotes: 0

Views: 49

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

Put your buttons in List or array and use a loop to add them to the container.

You could also create a method which used variable arguments

public void addAll(JComponent... comps) {
    for (JComponent comp : comps) {
        add(comp);
    }
}

Upvotes: 5

Related Questions