Painting JComponents with CellRendererPane
I wrote a class that paints arbitrary JComponent that way it looks like itself but without any specific behavior. For example I paint JButton. It looks like JButton but it is not highlighted when mouse moves over it. It ignores clicks on it etc. Its just an Image of JButton!
I use CellRendererPane (package javax.swing) for this effect. Here is my wrapper class for drawing arbitrary JComponents (not full version):
publicclass DesignerModeComponentRendererextends JPanel
{
protected JComponent representedComponent;
private CellRendererPane pane;
public DesignerModeComponentRenderer(JComponent representedComponent)
{
this.representedComponent = representedComponent;
pane =new CellRendererPane();
Dimension d = representedComponent.getPreferredSize();
setPreferredSize(new Dimension(d.width, d.height));
}
publicvoid paint(Graphics g)
{
Dimension d = representedComponent.getPreferredSize();
pane.paintComponent(g, representedComponent, this.getParent(), 0, 0, d.width, d.height,true);
}
}
As you can see it's just a JPanel with overriden paint(Graphics g) method where CellRendererPane draws representedComponent.
When I wrap JLabel, JButton, JTextField, JCheckBox with this class I reach desired effect. But when I use this class to draw JComboBox it is painted incorrectly. There is only Selected Option without arrow button that opens popup menu with options. And when I try to wrap JPanel with internal content elements I get just JPanel without any sign of its child components.
What's wrong? I'm sure that I'm on right way. But something losed in my method.
It seems that CellRendererPane must call paintChildren() method of representedComponent. But it does not for some reason.
Any suggestions?

