7

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
  • 6,027
  • 20
  • 57
  • 74

4 Answers4

30

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);
.....
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
5
jMenuBar1.add(Box.createHorizontalGlue());

and don't forget alignt JMenu with JMenuItem too

JMenu.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

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

    jMenuBar1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
OmAr
  • 97
  • 8
0

Your may refer to https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

Especially take a note on part

by putting horizontal glue between two components in a left-to-right box, you make any extra space go between those components

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
Park JongBum
  • 1,245
  • 1
  • 16
  • 27