aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Creating JPanels dynamicly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Creating JPanels dynamicly" Watch "Creating JPanels dynamicly" New topic
Author

Creating JPanels dynamicly

Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
Hello.

I want to make app which will have let say categories, they will be stored in database. For each category I want to have one JPanel (im using CardLayout so it will be one card). If I will add new category to my db, it will be added into JComboBox, and the new JPanel should be created and filled with other components. Is it possible to make?
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

Yes, it is possible.
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
Kathleen Angeles wrote:Yes, it is possible.


Can you tell me little more ? Any advice how to make it?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Any advice how to make it?

when you say this
"If I will add new category to my db, it will be added into JComboBox, and the new JPanel should be created and filled with other components. "

to me it reads that you already have the program working, except for the creation of the new category bit.

if you don't have anything working, you need to define what part is giving you problems

Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
Yes, I've got the GUI. I don't know how to write a method or listener which will create JPanel for each category from database, fill it with data/other components and and the end it should add this JPanel to the JPanel with cardLayout
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
is the new category added to the db via the gui?

if so, there's your listener - when added, also add to comboBox, create panel, add panel to cardlayout/revalidate/repaint
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18152
    
    8

You already have code elsewhere which creates a JPanel and populates it with components, don't you? I mean, you have a working GUI so you already have code like that. So you just use code which is just like your existing code. Once you have created the JPanel and populated it, you add it to the CardLayout. I don't understand why you are asking this question -- perhaps there is something else which is holding you up?
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
Michael Dunn wrote:is the new category added to the db via the gui?

if so, there's your listener - when added, also add to comboBox, create panel, add panel to cardlayout/revalidate/repaint


Yes, category will be added via the GUI. Maybe I didn't write it clearly. Category JPanel will have only ImageIcon components (all images (their paths) are stored in db).I will create JPanel in a listener "on add category" (is there any listener like that?). In that listener I will create new JPanel and add that JPanel to the CardLayout. JPanel it's empty (because you can't add Images to the category which wasn't exist). It's quite easy to this point.

Now I have method addImage. Each image is added to one category. How I will choose JPanel (with cateogry) to which it should add the image?

Maybe I will write some pseudo-code:


And to what I should add the category? JPanel with category was created in listener, how I can get reference to it ? Even If I would take the reference to "categoryPanel" as in the listener, it won't add the Image to the correct category (for example, there was created category Fantasy and Thrillers, and then I want to add a fantasy film to the fantasy category. if i would take the reference to categoryPanel it would add me that film to Thriller category (it was created as second)).

So now the question is, is there any "on add to db (or xml)" listeners, and how I can get reference to the correct cateogry?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18152
    
    8

How does code in one method get access to an object which isn't a class-level variable? You pass a reference to that object as a parameter, that's the easiest way.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18152
    
    8

Or alternatively:



Just have the method do one thing, namely creating a suitable image. Return that image so that the caller can do what it likes with it, in your case that would be adding it to the panel.
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
Paul Clapham wrote:How does code in one method get access to an object which isn't a class-level variable? You pass a reference to that object as a parameter, that's the easiest way.



Ok so what should I pass then? Because I can't pass categoryPanel right? because currently it will be a reference to a thriller category ;>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18152
    
    8

What should you pass? I have no idea. All I can see is some scraps of code with nothing to tie them together.

I suggest you go back to your original idea:

In that listener I will create new JPanel and add that JPanel to the CardLayout. JPanel it's empty (because you can't add Images to the category which wasn't exist).


And insert the code which adds components to the JPanel you just created.

If you have problems with that, post code which explains the problems. Preferably an SSCCE, but at least something which contains all the information necessary to explain the problems.
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
I mean how I can pass the correct JPanel in that method addImage(???) ? I can't add those images when I'm creating JPanel, because it's a category JPanel, and when it's created it has no images bound to that category.

I will try to post some code as soon as possible, maybe it will be easier to explain and solve the problem.

Best regards
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
possibly all you need to do is create a Category class (to add to comboBox)

get the name from the db
get that category's images from the db
in the class create a JPanel, with images from db
add that panel to the cardLayout panel, with 'name' as identifier
(either pass reference of cardLayout panel in Category constructor,
or make category class an inner class, so cardLayout panel is accessible)
in Category class, override toString() to return 'name'
add class instance to comboBox, which will display whatever's returned by toString()
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating JPanels dynamicly
 
Similar Threads
How can I add/remove user groups to access certain categories after they are created
Can I make a JPanel perform an action?
JPanel scroll bar
Tab Order
Question about GUI (with NetBeans IDE)