set JFrame Orientation from right to left!

Eng.Fouad picture Eng.Fouad · Jun 23, 2011 · Viewed 11k times · Source

To align my JFrame from righ-to-left, I use:

setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

but this works only if I use the following style (decoration) of the JFrame:

public class RightToLeft {
  public static void main(String []args){
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
      public void run() {
        try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); }
        catch (Exception e) { e.printStackTrace(); }
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("العنوان بالعربي");
        frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
      }
    });
  }
}

enter image description here

but I want it to work without this decoration. How to solve this issue?

EDIT:

@mre I want a JFrame like this one:

enter image description here

EDIT2:

I really really need this issue to be fixed, so I offer 500+ to who will give a JFrame like this (with WindowsLookAndFeel):

enter image description here

Answer

jfpoilpret picture jfpoilpret · Jun 27, 2011

The following explains what you observe through your code snippet:

ComponentOrientation is applicable only to Swing (and AWT actually) components.

When using JFrame.setDefaultLookAndFeelDecorated(true);, then the whole frame decoration is performed by Swing (the LookAndFeel itself in fact), so this works as expected.

If you don't set this option though, that means that the OS is in charge of the frame decoration, however the OS cannot be aware of the ComponentOrientation used by your Swing components!

I expect the OS (did you mention what OS you use exactly? It seems to be Windows 7 right?) to perform the correct decoration based on the currently selected Locale. Hence if you have an Arabic locale setup and selected on your OS, I guess all windows decorations are right to left. Did you try changing that Locale (through the "Region and Language" control panel)? Did it work?

Note: I don't think that you can change these OS settings directly from Java, but you can read them with Locale.getDefault().

To sum up:

  • first of all, you have to ensure that your OS is properly configured in terms of text orientation; sorry I can't help much here because I don't have any right-to-left languages installed on my Windows machine.

  • then, use the system look and feel and ensure that JFrame.setDefaultLookAndFeelDecorated(false);

  • if that doesn't work, then you may consider posting your code snippet, along with your system configuration to Oracle Java bugs list

What follows are extra notes on how to improve this part of your code (but this is not a fix for your issue)

By the way, if you let the user define its OS language preferences, then you shouldn't explicitly hard-code frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); but rather use something like:

frame.applyComponentOrientation(
    ComponentOrientation.getOrientation(Locale.getDefault()));

Where Locale.getDefault(), unless explicitly changed within your application, will return the OS-level currently selected Locale.

Also note that it is preferable to use applyComponentOrientation() rather than setComponentOrientation(), because the former recursively sets the given orientation to the whole hierarchy of components.

Finally, you will have to ensure in your windows that the LayoutManager used is right-to-left aware; this is normally OK with all standard Swing layouts, but not for all 3rd-party layout managers, if you use some.