15

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

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • @Eng.Fouad, I'm not sure I understand your question. It sounds like your want an undecorated `JFrame`... – mre Jun 23 '11 at 14:44
  • 1
    @Eng.Fouad, then instead of loading the cross platform look and feel, why don't you load the specific look and feel that you want to use? – mre Jun 23 '11 at 15:02
  • @mre I want to use `WindowsLookAndFeel` but when I use it, `setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);` doesn't work – Eng.Fouad Jun 23 '11 at 15:10
  • @Eng.Fouad, what do you mean by it doesn't work? do you have a stack trace or anything? also, are you running on a Windows OS? – mre Jun 23 '11 at 15:30
  • @mre I meant it doesn't affect on the direction of the JFrame (i.e the close button will be on the right corner) – Eng.Fouad Jun 23 '11 at 15:31
  • @Eng.Fouad, interesting....perhaps it's a bug? – mre Jun 23 '11 at 15:53
  • 1
    @Eng.Fouad, you should report it! :) – mre Jun 23 '11 at 15:57
  • 2 days left and no acceptable answer yet :( I hope someone comes with one – Eng.Fouad Jul 01 '11 at 01:42
  • If you want to higher your chances to get better answers, you will have to provide more information: what's your OS? what's your JDK? How is your OS configured regarding regional settings? Finally, providing a complete (runnable) sample that shows the problem would have others check it on their platform and report if the problem is everywhere or not; they could also then try to tune your example to make it work. – jfpoilpret Jul 01 '11 at 04:29
  • 1
    I think you may be out of luck with this one. Found the following bug that is marked as "11-Closed, Not a Defect" : [JFrame ComponentOrientation.RIGHT_TO_LEFT does not work on system look&feel](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6717765) – obscuredlogic Jul 02 '11 at 13:49
  • Anyone solve this issue, I will offer +500 for him – Eng.Fouad Jul 03 '11 at 13:01

6 Answers6

7

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
jfpoilpret
  • 10,449
  • 2
  • 28
  • 32
  • I tried `applyComponentOrientation(ComponentOrientation.getOrientation(Locale.getDefault()));` but it doesn't work :\ – Eng.Fouad Jun 28 '11 at 01:06
  • First of all, can you confirm that your OS Locale is arabic? You can add `System.out.println(Locale.getDefault());` to your code to confirm that. – jfpoilpret Jun 28 '11 at 04:38
  • What about `System.out.println(ComponentOrientation.getOrientation(Locale.getDefault());` ? – jfpoilpret Jun 28 '11 at 06:22
  • Can you confirm that all other frames (not Java applications) in the system use RTL orientation in the title bar? Besides, why do you use the cross-platform look and feel, why not use the system look and feel instead? – jfpoilpret Jun 28 '11 at 06:24
  • `System.out.println(ComponentOrientation.getOrientation(Locale.getDefault()).isLeftToRight());` gives `false ` – Eng.Fouad Jun 28 '11 at 08:52
  • How can I use RTL orientation in not-Java application? – Eng.Fouad Jun 28 '11 at 08:54
  • @jfpoilpret that's what I want `UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());` or `UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");` but both don't accept `setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);` nor `applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);` – Eng.Fouad Jun 28 '11 at 09:00
  • You say "both don't accept..." Do you mean that INSIDE the frame components are not Right to left? Or do you mean that the TITLE BAR is not RTL? In the second case, my answer tells you why: frames decoration are painted by the OS, not Swing itself. – jfpoilpret Jun 28 '11 at 09:27
  • Using RTL orientation should be a regional option at OS level. Unfortunately I can't help you much on that one because I don't have arabic (or any other RTL language) instealled on my OS. – jfpoilpret Jun 28 '11 at 09:27
  • Can you please also check that there is no explicit call to `Locale.setDefault(...);` in your Java code? I want to make sure that the OS-level Locale is really `ar_SA`, not something else. – jfpoilpret Jun 28 '11 at 09:30
  • @jfpoilpret: I meant the title bar is not RTL – Eng.Fouad Jun 28 '11 at 09:33
  • There is no explicit call to `Locale.setDefault(...);` in my code – Eng.Fouad Jun 28 '11 at 09:37
  • Please note that my answer is twofold: the first part explains what probably happens in your situation. The second part is made of additional notes that are not supposed to FIX the problem but imprvoe your code AFTER the problem is fixed. – jfpoilpret Jun 28 '11 at 11:45
  • I have just edited my answer to clarify these points and a few further details. – jfpoilpret Jun 28 '11 at 11:53
5

@Eng.Fouad

just joke and this one ???...

Substance L&F

code:

import java.awt.ComponentOrientation;
import javax.swing.JFrame;
import javax.swing.UIManager;
import org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel;

public class RightToLeft {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                    UIManager.setLookAndFeel(new SubstanceOfficeSilver2007LookAndFeel());
                } 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);
            }
        });
    }

    private RightToLeft() {
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Where can I download this package "org.pushingpixels.substance.api.skin"? – Eng.Fouad Jul 14 '11 at 19:19
  • @Eng.Fouad that's from old and original package http://java.net/projects/substance/, but during this year was forked and continue with name Insubstantial https://github.com/Insubstantial (for hardiest changes you have to use invokeAndWait instead of invokeLater http://stackoverflow.com/questions/6579087/gui-problem-with-java-substance-look-and-feel/6579925#6579925), here is usefull almanach for L&F http://stackoverflow.com/questions/3954616/look-and-feel-in-java, JGoodies has good implementations for CompoentOrientation, you have to try that, because I never ever needed change that – mKorbel Jul 14 '11 at 20:02
  • Could you please explain briefly how can I use this package? I'm not familiar with using pakages. I will give you the +50 rep because it's almost expired and your solution seems satisfied what I want – Eng.Fouad Jul 15 '11 at 04:25
  • 1
    just download and link or import this package as JDBC driver or any driver, examples are in code souce – mKorbel Jul 15 '11 at 07:38
3

I suspect it has to do more with the OS. Normally (if you don't call setDefaultLookAndFeelDecorated) it is the OS that provides the frame decoration, not the LAF.

You should try changing your preferences in the OS to say you want right to left orientation.

Sorry, I don't know where those settings would be. Once you do this, then you should be able to remove the call to setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); as the LAF will pick up the OS settings from the Locale.

EDIT

This Link describes how to enable right-to-left text on Windows 7. Then I think you would also need to change your locale.

wolfcastle
  • 5,850
  • 3
  • 33
  • 46
1

It looks like the Component Orientation feature is not supported with the Windows LookAndFeel (at least not for the title bar)

javagruffian
  • 607
  • 2
  • 7
  • 13
  • 2
    the title bar has nothing to do with the LAF - it's provided by the OS. True is that WindowsLAF doesn't support the LAFDecoration (aka: instead of using the default OS window decoration, let the LAF roll its own) – kleopatra Jul 08 '11 at 15:46
1

Here is one posibility. This utility is designed for Mac users who have switched to Windows and want the window buttons on the left, but it should serve the same needs as yours.

This solution has nothing to do with Java (so I don't know if it's even acceptable for your needs) and sounds like it would be external to your application. I have not been able to try it out myself (I'm not running Windows), so I can't vouch for it, but it might be worth a try.

wolfcastle
  • 5,850
  • 3
  • 33
  • 46
-1

You only need to add this line of code and I am sure it works 100%.

frame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

enter image description here

4b0
  • 21,981
  • 30
  • 95
  • 142
Ashraf Gardizy
  • 357
  • 4
  • 9