Running on Java 22-ea+27-2262 (Preview)
Home of The JavaSpecialists' Newsletter

099Orientating Components Right to Left

Author: Dr. Heinz M. KabutzDate: 2004-11-23Java Version: 1.4Category: GUI
 

Abstract: In Swing, we can make GUIs be oriented either from left to right, as most countries do, or from right to left, as you would find in Arabic and Hebrew.

 

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 :)

javaspecialists.teachable.com: Please visit our new self-study course catalog to see how you can upskill your Java knowledge.

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 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 5
    gui.setAlwaysOnTop(true);        // since JDK 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 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

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...