Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating JPanels dynamicly

 
Marek Krokosinski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is possible.
 
Marek Krokosinski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kathleen Angeles wrote:Yes, it is possible.


Can you tell me little more ? Any advice how to make it?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic