Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set JFrame Orientation from right to left!

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

like image 987
Eng.Fouad Avatar asked Jun 23 '11 14:06

Eng.Fouad


People also ask

What does JFrame setVisible do?

The setSize(400,300) method of JFrame makes the rectangular area 400 pixels wide by 300 pixels high. The default size of a frame is 0 by 0 pixels. The setVisible(true) method makes the frame appear on the screen.

How do I set JFrame to center of screen?

Just click on form and go to JFrame properties, then Code tab and check Generate Center .


4 Answers

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.

like image 148
jfpoilpret Avatar answered Sep 26 '22 09:09

jfpoilpret


@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() {
    }
}
like image 43
mKorbel Avatar answered Sep 22 '22 09:09

mKorbel


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.

like image 33
wolfcastle Avatar answered Sep 24 '22 09:09

wolfcastle


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

like image 41
javagruffian Avatar answered Sep 23 '22 09:09

javagruffian