Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2007

Tip: Looking for answers? Try searching our database.

JRadioButtonMenuItem text too far left + menu item text alignment

Thread view: 
Karsten Wutzke - 07 Jun 2007 09:08 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.