aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Multiple parameters in .add method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Multiple parameters in .add method" Watch "Multiple parameters in .add method" New topic
Author

Multiple parameters in .add method

WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 91
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: 40052
    
  28
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: 91
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: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple parameters in .add method