I know this question has been asked a million times, and can be done by overriding
paintComponent(), but what I want to know is how to change the shape while respecting the current Look And Feel. If I want to change the shape to a circle, I also want the button to look like a button, just with a different shape.
I tried making a JButton, the overriding
paintComponent, then clipping it to a circle, but I didn't get the border effects on
Is there a better way? Or is there a method in
For drawing components a ComponentUI is used. This UI class has a paint method and is used to paint the component. The paint method uses the settings of the L&F which are stored as properties in the UIManager.
To create your own component
RoundButton extending from e.g.
AbstractButton you can create your own
RoundButtonUI (maybe extending from
ButtonUI). Here you can create your own paint method using the properties of a normal button like
Button.foreground to draw your own component with the same L&F values as a normal button.
RoundButton class you should implement the method
getUIClassID() which will return the string
"RoundButtonUI". This causes your
RoundButtonUI to be used.
A good example is