This tip is about round Swing buttons. Actually, the information in this tip applies to a button of any arbitrary shape but to keep the discussion simple, we're just going to use a round shape.
There are two things you need to do when creating a round button. The first is to override the appropriate painting methods in order to paint the round shape. The second is to set things up so that the button responds only when you click within the round button (not just within the rectangle that contains the round button).
Here's an example program that implements a round button.
public class RoundButton extends JButton { public RoundButton(String label) { super(label);
// These statements enlarge the button so that it // becomes a circle rather than an oval. Dimension size = getPreferredSize(); size.width = size.height = Math.max(size.width, size.height); setPreferredSize(size);
// This call causes the JButton not to paint the background. // This allows us to paint a round background. setContentAreaFilled(false); }