| Author |
Multiple parameters in .add method
|
WeiJie Lim
Ranch Hand
Joined: Sep 05, 2012
Posts: 68
|
|
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
Joined: Oct 13, 2005
Posts: 32654
|
|
|
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
Joined: Sep 05, 2012
Posts: 68
|
|
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
Joined: Oct 13, 2005
Posts: 32654
|
|
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.
|
 |
 |
|
|
subject: Multiple parameters in .add method
|
|
|