views:

178

answers:

2

Hi,

I am trying to display a circular object in my gui, the circular object should contain a few labels therefore I thought the circle object should extend JPanel. Does anyone know how to make a circular JPanel? Or at least a JPanel which paints an oval and places a few JLables in the centre of the oval?

Thanks

+6  A: 

To paint a circle, subclass JPanel and override paintComponent:

public class CirclePanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        g.drawOval(0, 0, g.getClipBounds().width, g.getClipBounds().height);
    }
}

Looks like this:

alt text

To place the labels, you could use GridBagLayout, hope that's what you want:

CirclePanel panel = new CirclePanel();

panel.setLayout(new GridBagLayout());

GridBagConstraints gc;

gc = new GridBagConstraints();
gc.gridy = 0;
panel.add(new JLabel("Label 1"), gc);

gc = new GridBagConstraints();
gc.gridy = 1;
panel.add(new JLabel("Label 2"), gc);

alt text

Peter Lang
can you add labels to the centre of the oval?
Aly
A: 

In the book Swing Hacks from O'Reilly there is a hack for transparent and animated windows #41. The source code can be downloaded from http://examples.oreilly.com/9780596009076/

Kennet