Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap of swing component  RSS feed

 
Luigi Mattino'
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
i'm untrained with swing, and i need some help a practical question.
I have a javax.swing.JPanel and it is needed to manage more that one JPanel on it. At least three or four are inside this "Father JPanel". All of this JPanel has a unique code.
I thought to use an HashMap<UNIQUE_CODE, JPANEL> as a field of the "Father JPanel" in this way when i need to update data inside those panel i can recall it whith the unique code.


I ask for advice: It is a good practice?
or it is better to use getComponent and others method given from javax.swing.Component and manage.
Thanks in advantage.
 
Campbell Ritchie
Sheriff
Posts: 53766
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put Components into Maps, yes. But there is something not quite right about the approach.

I think you should use the GUI panels to display your data. Display, and nothing else. You should have an application which creates or stores the data, and buttons which call methods of the application (and may update your display). You should have the application running and doing all the manipulation of the data before you think of a GUI.

Should you want different panels in the same location which appear and disappear, you can do that with a specific Layout. Look for card layout.
 
Luigi Mattino'
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i actually use those JPanel to display data (the data string that is encoded), after i put this panel in hashmap i add it into the FatherPanel. And when i need to update data on CHildPanel i use load method.
My question is more rough about use an hashmap alternatively with the management of the JPanel itself. If i do so i need to iterate over all component inside the JPanel and compare the unicode and then update it.

I hope I have given a good explanation.
thanks.
 
Campbell Ritchie
Sheriff
Posts: 53766
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest that keeping the data in the GUI is a bad idea. You may want a Map where you retain the data elsewhere in your application, however.
 
Luigi Mattino'
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thank you for the advice.
 
Campbell Ritchie
Sheriff
Posts: 53766
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!