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?

[2381 byte] By [Bront_in_Hooda] at [2007-11-27 2:37:37]
# 1

I've solved the problem!

I reviewed JList class to learn how it uses CellRendererPane. Then I've made some changes in my wrapper class (DesignerModeComponentRenderer posted above). I tryed to make use CellRendererPane identic to JList use. After some changes my wrapper class start to draw JComboBox correctly. Here is result wrapper class code:

public class DesignerModeComponentRenderer extends JPanel

{

protected JComponent representedComponent;

private CellRendererPane pane;

public DesignerModeComponentRenderer(JComponent representedComponent)

{

this.representedComponent = representedComponent;

pane = new CellRendererPane();

pane.add(representedComponent);

add(pane);

Dimension d = representedComponent.getPreferredSize();

setPreferredSize(new Dimension(d.width, d.height));

}

public void paint(Graphics g)

{

Dimension d = representedComponent.getPreferredSize();

pane.paintComponent(g, representedComponent, this, 0, 0, d.width, d.height, true);

}

}

Now 'representedComponent' being added to 'pane', 'pane' added to DesignerModeComponentRenderer and in method pane.paintComponent() container parameter is 'this' (instead of 'this.getParent()' as in previous version).

I think that the roblem was with not punctual parent-child Component structure in my wrapper class.

Bront_in_Hooda at 2007-7-12 2:57:57 > top of Java-index,Desktop,Core GUI APIs...