Netbeans Platform RTL (Right To Left) Component Orientation

October 21st, 2009 | by Tonny Kohar |

If you haven’t aware Java have support for RTL (Right-To-Left) for the User Interface, and thank to Netbeans Platform based on Swing, it is easy as well to make Netbeans Platform application to support RTL (Right-To-Left) as well

Step 1: Create ComponentOrientation support class

This step is optional, but make the testing easier, because it use the value component orientation from System.getProperty(“..”), and use auto (based on Locale.getDefault() if nothing specified.

    public class ComponentOrientationSupport {
 
        private ComponentOrientationSupport() {
            throw new Error("ComponentOrientationSupport is a utility class for static methods"); // NOI18N
        }   
 
        /** 
         * Return component orientation base on the value of
         * {@code System.getProperty("alkitab.orientation")} <br/>
         * If it is not specified it will return ComponentOrientation.LEFT_TO_RIGHT
         * Possible value are:
         * <code>
         * - auto (Default), automatic setting based on Locale.getDefault()
         * - ltr, force to use Left to Right orientation
         * - rtl, force to use Right to Left orientation
         * </code>
         * @return ComponentOrientation
         */
        public static ComponentOrientation getComponentOrientation() {
            ComponentOrientation orient = ComponentOrientation.getOrientation(Locale.getDefault());
 
            String str = System.getProperty("component.orientation");
 
            if (str != null) {
                if (str.equals("ltr")) {
                    orient = ComponentOrientation.LEFT_TO_RIGHT;
                } else if (str.equals("rtl")) {
                    orient = ComponentOrientation.RIGHT_TO_LEFT;
                }
            }
 
            return orient;
        }
 
        /** Apply the getComponentOrientation to the specified container
         * @param container Container
         */
        public static void applyComponentOrientation(Container container) {
            container.applyComponentOrientation(getComponentOrientation());
        }
    }

Step 2: Apply the component orientation for the MainWindow

In order to apply the component orientation to the MainWindow, you need to do that in the ModuleInstall eg:

    @Override
    public  void restored() {
        super.restored();
 
        // initialize the orientation
        String orientationKey = "component.orientation";
        String strOrientation = System.getProperty(orientationKey);
        if (strOrientation == null) {
            strOrientation = "auto";
        } else {
            strOrientation = strOrientation.trim().toLowerCase();
        }
        System.setProperty(orientationKey, strOrientation); // re-setting the System property to the correct value
 
        // do something else
        //....
 
        WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
            public void run() {
                ComponentOrientationSupport.applyComponentOrientation(WindowManager.getDefault().getMainWindow());
 
                // do something else
                //....
            }
        });
    }

Step 3: Apply the component orientation for each TopComponent

You need to apply component orientation for each TopComponent you have eg:

    public final class MyTopComponent extends TopComponent {
        private MyTopComponent() {
            initComponents();
            setName(...);
            setToolTipText(...);
 
            // do something else
            //....
 
            ComponentOrientationSupport.applyComponentOrientation(this);
        }
    }

Step 4: Testing

Because it is using System.getProperty(“..”), so it is easy to test the UI. You only need to assign that component.orientation=auto|ltr|rtl to the environment. In Netbeans Platform based application you can do that in the suite project.properties by adding

run.args.extra=-J-Dcomponent.orientation=rtl ...your other things eg: -J-Xms64m -J-Xmx128m ...

Or for deploying you can use ../etc/yourapp.conf as above. Or if you are not setting it up, it will use “auto” by default which is based on Locale.getDefault()

Tags: , ,

  1. 4 Responses to “Netbeans Platform RTL (Right To Left) Component Orientation”

  2. By ola on Feb 3, 2014 | Reply

    hi
    i want to make netbeans platform language switcher language when choose Arabic the ui Oriantaion for all components should be RTL, and translate menu bar.
    when select English should make the oriantaion of ui is LTR.
    thanks

  3. By Sma on Mar 1, 2014 | Reply

    can you please clarify to me the first step in more details.

  1. 2 Trackback(s)

  2. Oct 21, 2009: Tweets that mention Inspiration and Expression » Blog Archive » Netbeans Platform RTL (Right To Left) Component Orientation -- Topsy.com
  3. Sep 4, 2012: 5 Tips for Arabic Java Desktop Developers | UmeedainTimes.com - Submit Your Tutorials

You must be logged in to post a comment.