Hello all!
In my JRadioButtonMenuItem's I set icons. When displayed in a menu
along with other JMenuItems or JMenus it's only the
JRadioButtonMenuItems TEXT that is displayed a few pixels (4?) to the
left. Like this, the alignment in the menu gets screwed and looks
really shabby.
1. I tried to fix it by subclassing JRadioButtonMenuItem and
overriding paintComponent. Inside I called g.translate(4, 0) to
translate 4 pixels right, however the whole painting is shifted right.
What do I have to do to only display the text a little to the right?
2. I managed alignment of menu items by setting transparent icons to
those which need alignment. I just wanted to hear whether this is the
correct way to do so or not. Is there a standard/better way?
TIA
Karsten
Karsten Wutzke - 13 Jun 2007 14:30 GMT
> Hello all!
>
[quoted text clipped - 15 lines]
> TIA
> Karsten
For anybody experiencing JRadioButton alignment problems using
transparent images use the following code snip to create a new
ImageIcon that is X pixels wider to the left edge (not using scaling,
just simple translation):
-----------
ImageIcon iiSmall = new ImageIcon(...); //whatever constructor
int numPixels = 3; //adjust by x pixels
Image img = iiSmall.getImage();
//System.out.println("Creating correct alignment icon for '" +
ac.getKey() + "' (" + img.getClass().getSimpleName() + "
instance)...");
int w = iiSmall.getIconWidth();
int h = iiSmall.getIconHeight();
BufferedImage bi = new BufferedImage(w + numPixels, h,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();nu
/*Color clr = g2.getColor(); //for debugging only
g2.setColor(Color.GRAY);
g2.fillRect(numPixels, 0, w, h);
g2.setColor(clr);*/
g2.drawImage(img, numPixels, 0, null);
g2.dispose();
iiSmall = new ImageIcon(bi);
-----------
Cya
Karsten
Karsten Wutzke - 13 Jun 2007 15:11 GMT
Typo alert! This is all about JRadioButtonMenuItem's of course...
Karsten
Karsten Lentzsch - 14 Jun 2007 18:57 GMT
> In my JRadioButtonMenuItem's I set icons. When displayed in a menu
> along with other JMenuItems or JMenus it's only the
> JRadioButtonMenuItems TEXT that is displayed a few pixels (4?) to the
> left. [...]
This depends on the Look&Feel you are using; for core L&fs
it depends on the Java runtime version you are using.
It helps others understand your context, if you provide
exact information about the Java vendor, Java version,
Java build no, L&f used, platform, platform version.
For example Sun Java 1.6.0 update 1, Sun Windows L&f,
Windows XP.
If you are using the Sun Windows L&f, you may try to
update your Sun Java environment, or may try the JGoodies
Windows L&f. The latter addresses many issues in the
micro design, including the menu item alignment.
-Karsten