Home of The JavaSpecialists' Newsletter

099Orientating Components Right to Left

Posted: 2004-11-23Category: GUIJava Version: 1.5+Dr. Heinz M. Kabutz
 

Abstract: 

 

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

Birthday and Anniversary Special: Since the 30th November 2017 is the 17th anniversary of our newsletter and my birthday is coming up on the 4th December, we are giving away a 30% discount on our new Data Structures for Java 9 Course (Late 2017 Edition). Whether you are a seasoned Java programmer or you just want to get ready for your next job interview, this course will help you. Besides detailed lectures, the course has over 130 questions that will help you discover what you missed.

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

 

Related Articles

Browse the Newsletter Archive

About the Author

demo

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

Java Training

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

Java Consulting

Nobody ever wants to call a Java performance consultant, but with first-hand experience repairing and improving commercial Java applications - JavaSpecialists are a good place to start...

Threading Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.