This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I have an application that uses panels to display data. This can grow into thousands of panels all of the same type. I've been looking into using the flyweight design pattern to lower system resources and make the application faster all around. Can you use the flyweight pattern with JPanels? I've been attempting to do so but haven't had any luck thus far. I've created the factory which works but it only displays the 1 panel which happens to be the last in the list created. Below are some of the requirements I need
1. Each panel has the same appearance 2. Each panel has a different question object attached which holds information about what to display inside the panel 3. Each panel has icons being displayed based on conditions being passed.
What would be my best route for this type of situation? Thanks for the help.
In general a Component object can only be in one Container object. If you add the same Component to a different Container, it will disappear from the first Container. This is what I call the "Houdini Effect".
Joined: Jan 21, 2003
So basically using the flyweight with Panels or any component that needs to have multiple nested components there is no other way other than creating several instances of that component if you want it inside the same nested component correct?
Both JTree and JTable use the concept of "Renderers" and "Editors" to do something similiar to the flyweight pattern you're trying.
It sounds like using JTable might work for the situation you describe. The table's data model would be the backing information - the question object and the "conditions being passed". You would create a class that implements TableCellRenderer to provide a graphical display for this information, and a class that implements TableCellEditor to provide a way for the user to change the information.
Java Tutorial section on Table renderers and editors
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Components such a text components have a related objects that could be re-used. For example, text components have a related Document object, DocumentFilter, and InputerVerifier that might be re-usable.