Original Post
I wanted to make a button with rounded edges or that I could basically do anything with graphically, so I made a new class like the one below:
public class ColorButton extends JButton {
public ColorButton(String text) {
super(text);
// We must be non-opaque since we won't fill all pixels.
// This will also stop the UI from filling our background.
setOpaque(false);
setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
}
protected void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
g.setColor(Color.black);
g.fillRect(0,0, width, height);
// Paint a rounded rectangle in the background.
g.setColor(Color.orange);
g.fillRoundRect(0, 0, width, height, height, height);
// Now call the superclass behavior to paint the foreground.
super.paintComponent(g);
}
}
This works well until you click on the button, at which point it must be calling a different painting method or something, because it goes back to its default pressed look, until you let go.
Any ideas? or are there other better ways to achieve this?
[edited by - dovyman on April 26, 2004 9:09:01 PM]