Rob Oxspring Rob Oxspring - 10 days ago 6
Java Question

How do I create a custom JPanel extension and use it from the NetBeans palette?

I have a

JPanel
extension that I've written and would like to be able to use it in the NetBeans designer. The component is simply adds some custom painting and continues to function as a container to be customized on each use.

I have properties to expose in addition to the standard
JPanel
ones and have a custom
paintComponent()
method that I'd like to be able to see in use when building up GUIs. Ideally I'd like to associate an icon with the component as well so that its easily recognizable for my colleagues to work with.

What's the best way of achieving this?

Answer

I made JPanel component in NetBeans with overridden paint method:

@Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    ...
    //draw elements      
    ...
}

It has some custom properties accessible through NetBeans properties window.

public int getResolutionX() {
    return resolutionX;
}

public void setResolutionX(int resolutionX) {
    this.resolutionX = resolutionX;
}

public int getResolutionY() {
    return resolutionY;
}

public void setResolutionY(int resolutionY) {
    this.resolutionY = resolutionY;
}

I put it in my palette using: Tools->Palette->Swing/AWT Components.

It even has the same look I painted in my overridden paint method while I am doing drag/drop in another container. I didn't associate icon to it though.

Hope this helps.

Comments