|
Orientating Components Right to Left
2004-11-23 The Java Specialists' Newsletter [Issue 099] -
Orientating Components Right to Left
Author:
Dr. Heinz M. Kabutz JDK version: Sun JDK 1.5.0-b64
If you are reading this, and have not subscribed, please consider doing it now by going to our
subscribe page. You can subscribe either via email or RSS.
Welcome to the 99th edition of The Java(tm) Specialists' Newsletter. Last week, my mom
and I went to a conference on the South African Economy, at
which our finance minister (Trevor Manual) also spoke. It
amused me that almost all the economists predicted
different interest rates for 2005 :) It amused me even more
when I read about this conference the next day in the Cape
Times, in which a reporter wrote that "280
business leaders and academics" attended the
conference. Hey, Mutti, we are numbered amongst those 280
business leaders :-) [then again, my mother is
the CEO of a highly successful drinking straw factory :]
After 9/11, our currency went for a major nose-dive, and
stopped sinking at about 13.5 Rand to the USD. It has been
steadily gaining since, so that now we are at R5.84 to the
greenback. Trevor Manual has even suggested that he would
welcome
a weaker rand, since having a strong currency hurts
the exporters. I would also prefer a weaker Rand, just not
in the next few months.
This week is particularly interesting, as I am teaching Java
at the Institute for Satellite & Software Applications
in South Africa, where the previous apartheid government
wanted to send satellites into space. It is eerie walking
around the enormous building, full of empty space, and
thinking of the tax money that went into that project, and
then contemplating what would have happened if we had not
dismantled the project of building long-distance rockets with
nuclear capabilities...
Enough history about what is happening in this distant
"developing" country, and over to Java :)
Orientating Components Right to Left
This newsletter will be quite short, I promise. A few months
ago, I was reading someone's code, when I noticed that they
had used the constraint BorderLayout.LINE_START instead of
the more conventional BorderLayout.WEST. This caused me to
investigate, and I soon discovered that there is a class
called ComponentOrientation, which I was not aware of.
Is it not amazing how we can miss subtle details like that?
It turns out that ComponentOrientation allows you to arrange
components either from Left to Right or from Right to Left,
depending on the Locale that you are at, especially the
language. The middle eastern languages are usually written
from Right to Left. There is also the provision to allow
languages that are written from Top to Bottom, but that is
currently not supported.
I suppose the idea is that Java GUIs should be laid out in
such a generic fashion, that you can switch between English
and Arabic without changing a line of code.
There is a fairly complete article written on this topic
on
IBM's website.
Here is my example, which demonstrates component orientation.
Whilst writing the example, I discovered two new methods in
JDK 1.5: Window.setLocationByPlatform(boolean) and
Window.setAlwaysOnTop(boolean):
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame {
public GUI(ComponentOrientation orientation) {
super(orientation.isLeftToRight() ? "Left to Right" : "Right to Left");
add(makeLabel("PageStart"), BorderLayout.PAGE_START);
add(makeLabel("LineStart"), BorderLayout.LINE_START);
add(makeLabel("LineEnd"), BorderLayout.LINE_END);
add(makeLabel("PageEnd"), BorderLayout.PAGE_END);
add(makeLabel("Centre"));
applyComponentOrientation(orientation);
}
private JLabel makeLabel(String text) {
JLabel label = new JLabel(text);
label.setBorder(BorderFactory.createRaisedBevelBorder());
return label;
}
private static void showGui(ComponentOrientation orientation) {
GUI gui = new GUI(orientation);
gui.setSize(640, 480);
gui.setLocationByPlatform(true); // since JDK 1.5
gui.setAlwaysOnTop(true); // since JDK 1.5
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
gui.setVisible(true);
}
public static void main(String[] args) {
showGui(ComponentOrientation.LEFT_TO_RIGHT);
showGui(ComponentOrientation.RIGHT_TO_LEFT);
}
}
I particularly like the new JDK 1.5 functionality that lets
me position the window according to the platform default.
It has annoyed me for the last 7.5 years, that the Frames
either appear in the top left-hand corner, or that they
appear centered. One other approach is to automatically save
the Frame positions and then restore them when the Frame is
first shown.
That is it for this week. Next week, I am aiming to send
the 100th edition of The Java(tm) Specialists' Newsletter on our 4th anniversary, the 30th
of November 2004. Let's see if I make it :)
Kind regards
Heinz
This material from The Java(tm)
Specialists' Newsletter by Maximum Solutions (South Africa). Please contact Maximum
Solutions for more information.
|