Swing menu item on right of Menu Bar

Romain Hippeau picture Romain Hippeau · Jul 28, 2011 · Viewed 7.6k times · Source

I have a swing application and on the JFrame's menu I want to add a Help MenuItem, but have it Right justified.
Any ideas ?

A Swing JMenuBar has a BoxLayout and I have tried:

menuItem = new JMenuItem("Help");
menuItem.setAlignmentX(Box.RIGHT_ALIGNMENT);
menuBar.add(menuItem);

The menu just stays on the left. I have also tried:

menuBar.add(Box.createHorizontalGlue());  

as per the Swing Tutorial... but that just adds a space.

I am using Windows 7. JDK 1.6.26

EDIT: It works as per the Java Tutorial if I do:

    menuBar.add(Box.createHorizontalGlue());

    helpMenu = new JMenu("Help");
    menuBar.add(helpMenu);
    menuItem = new JMenuItem("Help");
    helpMenu.add(menuItem);  

But that is not what I am looking for. I just want to be able to add the help MenuItem to the JMenuBar. For now that will need to be my fallback.

Answer

user802421 picture user802421 · Jul 28, 2011

Try Component.setComponentOrientation() method.

menuItem.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);