37172

Change JButton Shape while respecting Look And Feel

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?

Answer1:

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.

Recommend

  • Custom swing JTextComponent [closed]
  • Error when submit topology in Nimbus
  • Serializing a JFrame and sending via network
  • Event Dispatch Thread divided from logic thread,prevent blocking UI
  • Specifying a frame icon on Scala’s Swing Frame
  • Trigger animation when div element appears in viewport
  • Set Mouseposition in WPF [duplicate]
  • design time anomaly in window builder and swing
  • JSplitPane + MiGLayout: how to enable autoresizing
  • Is it possible to determine the size of the default framebuffer using OpenGL only?
  • JTextPane: KeyBindings are not working on StyledEditorKit
  • setSelected() with JRadioButton r[]=new JRadioButton[3] not working [duplicate]
  • Why do GeoJSON features appear like a negative photo of the features themselves?
  • Does “internal” visibility modifier in Kotlin work yet?
  • Jquery resizable reposition handle after scroll
  • dismiss Progress Dialog in another Activity … Android
  • Unable to click on the next page button containing “>” sign
  • Can long-polling be achieved in Restlet by just making the thread sleep?
  • What is the default HTTP verb in WebApi ? GET or POST?
  • jquery code not working without breakpoint
  • ckeditor and jquery UI dialog not working
  • Java Application vs. Java Desktop Application in Netbeans [duplicate]
  • apply a javascript function to draggable copy
  • Implement JwtBearer Authentication in NSwag SwaggerUi
  • Spring integration inbound-gateway Fire an event when queue is empty
  • jQuery: add elements until a particular height is reached
  • uml Composition relationships to RDF and OWL
  • Azure table store snapshot/backup capability
  • How do I include a SWC in an AS2 Flash project?
  • How to add a focus style to an editable ComboBox in WPF
  • Zurb Foundation _global.scss meta styles for js?
  • xtable package: Skipping some rows in the output
  • how to display data from 1st point on words on y axis for line chart in d3.js
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Change JButton Shape while respecting Look And Feel
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Updating server-side rendering client-side
  • jquery mobile loadPage not working
  • jQuery tmpl and DataLink beta
  • Unable to use reactive element in my shiny app