Reputation:
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 Nimbus LAF
.
Is there a better way? Or is there a method in JButton
?
Upvotes: 2
Views: 1054
Reputation: 486
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.font
or Button.foreground
to draw your own component with the same L&F values as a normal button.
In your 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 JButton
itself.
Upvotes: 1