• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple parameters in .add method

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, if I want to add 2 JButtons and 2 JRadioButtons, I have to

panelName.add(but1);
panelName.add(but2);
panelName.add(Jbut1);
panelName.add(JBut2);

It is tedious. Is there any way to simplify this process if I were to add many components at a time to the same JPanel ?

I tried panelName.add(but1,but2,JBut1,Jbut2); , but it doesn't work.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The add() method is overloaded, but not to add multiple buttons like that. You could try putting the buttons in an array and iterating it, adding them as you go. That might make it awkward to set layout constraints, however.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The add() method is overloaded, but not to add multiple buttons like that. You could try putting the buttons in an array and iterating it, adding them as you go. That might make it awkward to set layout constraints, however.


Thanks, never thought about using arrays.

Hopefully I wouldn't encounter the layout constraints you mentioned
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to post earlier and something went wrong.
You can add 10 buttons to make a keypad for a calculator like this.
Dependencies: constraints is an instance of a class rather similar to Cay Horstmann’s GBC class. The createNumberedButton method is a factory method which returns a button for calculators, complete with text, listener, etc. Or, maybe better, an Action. All set up by the method.My simple example puts the "0" button at the bottom left.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic