2

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?

  • 2
    [`PieceButton`](http://stackoverflow.com/questions/3066590/gui-problem-after-rewriting-to-mvc/3072979#3072979) has a round `Icon` that may get you started on your [sscce](http://sscce.org/). – trashgod Oct 23 '11 at 04:35
  • 4
    A component is unique for every LAF. So the shape/size/color/font is unique for every LAF. If you want to change the shape of a component then you need to rewrite the UI for the given LAF. Unless your LAF supports a Circle Border then you won't be able to do this. There is no shortcut. You need to write all the code. – camickr Oct 23 '11 at 04:38
  • @camickr How do you do that without knowing the current `LAF`??? –  Oct 24 '11 at 20:40

1 Answers1

1

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.

OblongZebra
  • 466
  • 5
  • 16
  • I guess this could be it although it still doesn't make the shaped button look perfect –  Nov 25 '11 at 01:06
  • I used it a lot when we made a new L&f, you have to take care all the border and insets etc. are correct, to make the LayoutManager handle the component correctly. Maybe you can post your code. – OblongZebra Mar 09 '12 at 10:16
  • Thanks, I got it with some tweaking. I think it look nice, and it still keeps the border effects thanks to how `Nimbus` is written! –  Mar 11 '12 at 21:21