2

I met problem to align icons of different sizes with JRadioButtonMenuItem. Here's a picture of my problem. The icons have several sizes and are left aligned.

The icons have several sizes and are left aligned

I would like to center the icons as in the picture below.

enter image description here

Apparently, none of the methods of JRadioButtonMenuItem class allow to do that...

paranoia25
  • 626
  • 1
  • 5
  • 23

2 Answers2

2

Calculate how much white space to add before and after an icon with the width of the widest icon and place each icon with the method setIconTextGap() such that:

[(widestIconWidth-iconWidth)/2_ofWhiteSpace][icon][(widestIconWidth-iconWidth)/2_ofWhiteSpace]
lebryant
  • 351
  • 2
  • 18
  • Ok, but how set the position of the icon? I don't see any method of the JRadioButton class which allows to do that? (In fact I use the JRadioButtonMenuItem class) – paranoia25 Sep 03 '12 at 15:21
  • Because there is no method. Just create a simple method by yourself to calculate how much white space to add before and after an icon. Then use method iconTextGap – lebryant Sep 03 '12 at 15:26
  • In fact the solution doesn't work. All elements are slightly shifted. It seems the layout of the radio button it's more complicated. – paranoia25 Sep 04 '12 at 10:06
  • Yep I see now, it makes the gap between icon and text ONLY, knowing that radiobutton has both of them. An alternative showed above really works. Also there's another way around - create icons (e.g. in Photohsop) such that all of them have the same width, graphics of interest centered and background set to 100% opaque. – lebryant Sep 04 '12 at 14:57
2

As an alternative, implement the Icon interface using a constant enclosing size and scale the rendered content to reflect the desired image size, as shown in this example. For convenience, you can render arbitrary sized unicode glyphs, as shown here.

image

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
trashgod
  • 203,806
  • 29
  • 246
  • 1,045