JMenu alignment in the right corner of JMenuBar in Java Swing

So, if I have JMenu and JMenuBar such that:

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

while the "About" menu is aligned on the left side of the menu bar. Is there anyway that I can align this menu to the right side of the menu bar?

+7
source share
4 answers

A fix is ​​available for this:

 jMenuBar.add(Box.createHorizontalGlue()); 

Add this line before adding the menu to the menu, and your menu will appear on the right side of the menu. Something like:

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

and don’t forget that alignt JMenu with JMenuItem too

 JMenu.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
+5
source

as mKorbel said for JMenu , he works on JMenuBar as follows:

  jMenuBar1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
+2
source

You can refer to https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

Especially pay attention to "by placing horizontal glue between two components in the field from left to right, you produce additional space between these components."

0
source

All Articles