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);
}
});
}
}
but I want it to work without this decoration. How to solve this issue?
EDIT:
@mre I want a JFrame like this one:
EDIT2:
I really really need this issue to be fixed, so I offer 500+ to who will give a JFrame like this (with WindowsLookAndFeel):
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.
Just click on form and go to JFrame properties, then Code tab and check Generate Center .
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.
@Eng.Fouad
just joke and this one ???...
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() {
}
}
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.
It looks like the Component Orientation feature is not supported with the Windows LookAndFeel (at least not for the title bar)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With