Aligning JMenu on the right corner of JMenuBar in Java Swing

20,386

Solution 1

There is a patch available for this:

jMenuBar.add(Box.createHorizontalGlue());

Add this line before adding menu to menubar and your menu will come on right side of menubar. Something like:

.....
jMenu1.setText("About");
jMenuBar1.add(Box.createHorizontalGlue()); <-- horizontal glue
jMenuBar1.add(jMenu1);
.....

Solution 2

jMenuBar1.add(Box.createHorizontalGlue());

and don't forget alignt JMenu with JMenuItem too

JMenu.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 

Solution 3

as mKorbel said for the JMenu it works on a JMenuBar like this :

    jMenuBar1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Share:
20,386
Johnydep
Author by

Johnydep

Updated on July 21, 2022

Comments

  • Johnydep
    Johnydep almost 2 years

    So if i have a JMenu & JMenuBar defined such that:

    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenu1.setText("About");
    jMenuBar1.add(jMenu1);
    
    // Finally
    setJMenuBar(jMenuBar1);
    

    and with this the Menu "About" is aligned to the left most side of the menu bar. Is there anyway that i can align this menu on the right most side of the menu bar?

  • Johnydep
    Johnydep over 12 years
    does that make any difference? As without using ComponentOrienatation it works just fine?
  • mKorbel
    mKorbel over 12 years
    JMenuItems are always placed inside Containers
  • kleopatra
    kleopatra over 12 years
    -1 for the second part: ComponentOrientation has nothing to do with the requirement (if I understand the OP correctly, s/he has a simple layout problem ;-)