aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Need to dynamically add checkBoxes in a GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need to dynamically add checkBoxes in a GUI" Watch "Need to dynamically add checkBoxes in a GUI" New topic
Author

Need to dynamically add checkBoxes in a GUI

Roshan Khandelwal
Ranch Hand

Joined: Apr 16, 2009
Posts: 35
Hi everyone ,
I have encountered a issue or better said technical ignorance roadblock.

I need to be able to add 'x' number of JPanel dynamically in a GUI window.
(Each of these JPanel further have a checkBox,JLabel and three JRadioButtons.)

By Dynamically i mean , the code runs... finds out that we need to add , say 10 JPanels and after the code has run or maybe even within the run , these JPanels are added to the GUI .

Any help or tutorial link would be appreciated.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

What have you got so far and where are you stuck?

Usually this is the way its done (in pseudo code)
1) Define parent panel. Set appropriate layout to the parent
2) Create the child components
3) Add the children to the parent
4) After adding all the children, call parent.revalidate() and parent.repaint()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rohit Kedia
Ranch Hand

Joined: Apr 24, 2010
Posts: 79
Hi there ,
I had a Similar requirement here , I was able to add child Panels(which consisted of some Components) to a Parent Panel , but problem was that after adding say 10 Panels(which actually depends on my Panel size , which could fit 10 child Panels say) all the newly added child Panels were getting squeezed within that . I had kept that Parent Panel in a ScrollPane but it didnt work.

I tried to increase Panel size every time I add a child Panel still it didn't work.

Note:
I was using a Button to add child Panels.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Rohit Kedia wrote:
I was able to add child Panels(which consisted of some Components) to a Parent Panel , but problem was that after adding say 10 Panels(which actually depends on my Panel size , which could fit 10 child Panels say) all the newly added child Panels were getting squeezed within that . I had kept that Parent Panel in a ScrollPane but it didnt work.
I tried to increase Panel size every time I add a child Panel still it didn't work.


If you nest components and try out different layout managers, this can be made to work without difficulty. For instance one way (and I'm not saying it's the best way, just that it works), is to place the check box containing JPanels into a GridLayout(0, 1) using JPanel, that is placed BorderLayout.NORTH into a BorderLayout using JPanel, that is placed into a JScrollPane.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> (Each of these JPanel further have a checkBox,JLabel and three JRadioButtons.)

if the panels are identical (some sort of quiz?), do they all need to be visible together?

if not, perhaps adding them as a CardLayout might suit i.e. next, next, next etc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to dynamically add checkBoxes in a GUI