This discussion is archived
4 Replies Latest reply: Jan 18, 2012 11:38 AM by martinog RSS

Why doesn't Swing automatically use correct component orientation?

martinog Newbie
Currently Being Moderated
Hello,

Why, in the following code, do I need to explicitely call applyComponentOrientation in order to see the menu bar show with right to left orientation for the Arabic language? I thought that just setting the default locale would be sufficient & the Swing code would use the correct component orientation automatically.
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class TestJMenuBar
{
     static public void main(String[] args) throws Exception
     {
          Locale.setDefault(new Locale("ar"));
          
          SwingUtilities.invokeLater(new Runnable() {
               public void run()
               {
                    JFrame frame = new JFrame("test");
                    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
                    
                    JMenuBar menuBar = new JMenuBar();

// Why is this necesarry?
menuBar.applyComponentOrientation(ComponentOrientation.getOrientation(Locale.getDefault()));

                    menuBar.add(new JMenu("File"));

                    frame.setJMenuBar(menuBar);

                    frame.setSize(450, 260);
                    frame.setVisible(true);
               }
          });
     }
}
Thanks
  • 1. Re: Why doesn't Swing automatically use correct component orientation?
    801313 Explorer
    Currently Being Moderated
    Things like this crop up all over the place. but anyway, sort answer:

    Yes, you are right. There are places in standard java where things could have probably been designed better, but in general it is good practice not to make any assumptions, to test things out yourself, and to adapt to what you are given, unless something is egregiously broken.
  • 2. Re: Why doesn't Swing automatically use correct component orientation?
    martinog Newbie
    Currently Being Moderated
    Thanks for the reply.

    So, from what I'm seeing, component orientation is one aspect of Java localization support that needs a lot of code changes to work. It looks like none of the components that support component orientation handle it automatically (not just JMenuBar).

    I got the list of Swing components that support component orientation from here:
    http://java.sun.com/javase/technologies/core/basic/intl/faq.jsp#component-orientation-support

    In addition to JMenuBar, I also tested JLabel:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    
    public class TestJLabel
    {
         static public void main(String[] args) throws Exception
         {
              Locale.setDefault(new Locale("ar"));
              
              SwingUtilities.invokeLater(new Runnable() {
                   public void run()
                   {
                        JFrame frame = new JFrame("test");
                        frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    
                        JLabel label = new JLabel("testlabel");
    
    // Need to explicitly set component orientation:
    label.applyComponentOrientation(ComponentOrientation.getOrientation(Locale.getDefault()));
    
                        frame.add(label);
    
                        frame.setSize(450, 260);
                        frame.setVisible(true);
                   }
              });
         }
    }
    And I tested JFileChooser:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    
    public class TestJFileChooser
    {
         static public void main(String[] args) throws Exception
         {
              Locale.setDefault(new Locale("ar"));
              
              SwingUtilities.invokeLater(new Runnable() {
                   public void run()
                   {
                        JFileChooser fc = new JFileChooser();
                        
    // Need to to explicitly set component orientation:
    fc.applyComponentOrientation(ComponentOrientation.getOrientation(Locale.getDefault()));
    
                        fc.showSaveDialog(null);
                   }
              });
         
         }
    }
    I can accept that Swing component orientation doesn't work without calls to java.awt.Container.applyComponentOrientation. But it is very inconvenient that this method will need to be called for each and every Swing component (or I'll need to write a method to traverse child components & then call applyComponentOrientation). It just seemed strange to me, given that this functionality is mentioned in the Oracle java tutorial pages:

    http://docs.oracle.com/javase/tutorial/ui/features/i18n.html

    Since it is mentioned in the tutorial I thought that the reason I wasn't seeing it applied automatically was because I was missing something, or doing something wrong.

    Thanks.

    Edited by: martinog on Jan 16, 2012 3:05 PM

    Edited by: martinog on Jan 16, 2012 3:10 PM
  • 3. Re: Why doesn't Swing automatically use correct component orientation?
    801313 Explorer
    Currently Being Moderated
    I can accept that Swing component orientation doesn't work without calls to java.awt.Container.applyComponentOrientation. But it is very inconvenient that this method will need to be called for each and every Swing component (or I'll need to write a method to traverse child components & then call applyComponentOrientation). It just seemed strange to me, given that this functionality is mentioned in the Oracle java tutorial pages:
    Very inconvenient? I think that might be over dramatic. I have written you a helper method which hopefully should make it a little bit more convenient, especially if you are using swing, which all decend from JComponent, which is a Container
         public static Container applyCorrectOrientation(Container c) {
              c.applyComponentOrientation(ComponentOrientation.getOrientation(Locale.getDefault()));
              return c;
         }
  • 4. Re: Why doesn't Swing automatically use correct component orientation?
    martinog Newbie
    Currently Being Moderated
    Thanks tjacobs01.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points