3

I am wondering if it's possible to change the default radio button menu item appearance in Java Swing.

By default a circle with a dot inside will indicate the selected state of the button, but i just want the good old fashioned tick next to a selected menu item and nothing to be shown next to an item that is not selected. (All items in question are of type JRadioButtonMenuItem)

I tried to use .setSelectedIcon(...) which can be found here :

http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setSelectedIcon(javax.swing.Icon)

But nothing changed, no exceptions thrown and im still stuck with the default appearance.

Any ideas?

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Dany Khalife
  • 1,850
  • 3
  • 20
  • 47

1 Answers1

6

Instead of using a JRadioButtonMenuItem, you could use JCheckBoxMenuItem which has a tick mark by default. JCheckBoxMenuItems can also belong to a ButtonGroup also giving you single selection behavior shown by JRadioButtonMenuItem.

Example

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • @HovercraftFullOfEels : Cann't we implement the same behaviour with `JRadioButtonMenuItem`, as shown in your example by `JRadioButton`. Never tried it, though the idea presented by you looks encouraging. – nIcE cOw Jan 27 '13 at 05:09
  • thanks for the suggestion, yes you can do it with radio buttons and that's what i have for now. the checkbox item is ok, but i am gonna wait to see if there is a way to put just a tick (with no box) before accepting your answer – Dany Khalife Jan 27 '13 at 05:16
  • @DanyKhalife I tested the `setSelectedIcon` on `JRadioButtonMenuItem` and did see the behavior (or lack of) you described. Thought this was a simpler solution :) – Reimeus Jan 27 '13 at 12:26
  • yea that's weird, the icon path should also be relative to the classpath correct? i agree, this does the trick for now :) – Dany Khalife Jan 27 '13 at 16:41
  • Yep, normally icon paths are read using `getResource()`. See this [post](http://stackoverflow.com/questions/31127/java-swing-displaying-images-from-within-a-jar) – Reimeus Jan 27 '13 at 21:01