1

I have a jMenuBar1 and there are two jMenu items (say jMenu1 and jMenu2) added to the jMenuBar1.

I managed to align the jMenuBar1 object with this line of code :

jMenuBar1.add(Box.createHorizontalGlue());    

Now I am trying to align the text in jMenu Items to right and making the text RTL(Right to Left) but nothing seems to work. I have already tried

Menu1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
jMenu1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

and no luck.

any solution will be highly appreciated.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
MoienGK
  • 4,544
  • 11
  • 58
  • 92

1 Answers1

1

You might need to give us more information maybe some screen shots of what you have and what you are trying to achieve as menu.setComponentOrientation() seems to work fine for me...

I get this

enter image description here

running this

import javax.swing.*;
import java.awt.*;

public class MenuTest extends JFrame{

    public static void display()
    {
        JFrame frame = new JFrame();
        frame.setSize(400,400);
        frame.setLayout(new GridLayout(6,1));
        JMenuBar menuBar = new JMenuBar();
        menuBar.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        JMenu jMenu1 = new JMenu("Menu1");
        jMenu1.setMinimumSize(new Dimension(200, 20));
        jMenu1.setMaximumSize(new Dimension(200, 20));
        jMenu1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        JMenu jMenu2 = new JMenu("Menu2");
        jMenu2.setMinimumSize(new Dimension(200, 20));
        jMenu2.setMaximumSize(new Dimension(200, 20));
        JMenuItem menuItem1 = new JMenuItem("Item1");
        JMenuItem menuItem2 = new JMenuItem("Item2");
        menuItem1.setMinimumSize(new Dimension(200, 20));
        menuItem1.setMaximumSize(new Dimension(200, 20));
        menuItem1.setPreferredSize(new Dimension(200, 20));
        menuItem1.setSize(new Dimension(200, 20));
        menuItem2.setMinimumSize(new Dimension(200, 20));
        menuItem2.setMaximumSize(new Dimension(200, 20));
        menuItem1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        menuItem2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        jMenu1.setHorizontalAlignment(JMenu.TRAILING);
        jMenu1.add(menuItem1);
        jMenu1.add(menuItem2);
        menuBar.add(jMenu1);
        menuBar.add(jMenu2);
        menuBar.setSize(frame.getWidth(), 20);
        frame.add(menuBar);
        frame.setVisible(true);
    }
    public static void main(String[] args)
    {
        EventQueue.invokeLater(
                new Runnable() {
                    @Override
                    public void run() {
                        MenuTest.display();
                    }
                }
         );
    }
}
Java Devil
  • 10,629
  • 7
  • 33
  • 48
  • 1
    unrelated to the question: don't [tweak the sizing hints](http://stackoverflow.com/q/7229226/203657) (aka: setXXSize) and don't do any manual sizing (aka: setSize) – kleopatra Jun 22 '13 at 13:11
  • related: guessing (no IDE handy to confirm) that you need not call setCO on each child if you first add them and then call applyCO on the toplevel parent – kleopatra Jun 22 '13 at 13:16
  • @kleopatra Re: sizing, Thanks for the hint, was more to point out the setCO working. Re: calling after added children, that was my thought also but when I tried that it didn't seem to propagate to children. – Java Devil Jun 22 '13 at 23:13
  • thanks for the info (and doing my work in checking my guess :-) – kleopatra Jun 23 '13 at 07:03
  • hmm ... actually, applyComponentOrientation worksforme in your example: first build the menubar without any tweaking of the menu/items, then call applyCO on the menubar and the menubar and the complete component hierarchy below appear in RToL as it should. BTW: a menubar has its own method frame.setJMenuBar – kleopatra Jun 23 '13 at 07:30
  • OK thanks, I didnt actually try applyCO..Even after suggesting it. Anyway hope this has all been somewhat of a help to the OP – Java Devil Jun 23 '13 at 07:40