my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Using Flyweight with Panels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using Flyweight with Panels" Watch "Using Flyweight with Panels" New topic
Author

Using Flyweight with Panels

Sloan Bowman
Ranch Hand

Joined: Jan 21, 2003
Posts: 107
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.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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".

Kaydell
Sloan Bowman
Ranch Hand

Joined: Jan 21, 2003
Posts: 107
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?
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I believe that this is true of AWT/Swing. I don't know about SWT.

Kaydell
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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.

Kaydell

Java Search
[ July 11, 2007: Message edited by: Kaydell Leavitt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Flyweight with Panels