This discussion is archived
1 Reply Latest reply: May 22, 2010 5:47 PM by 843810 RSS

trouble in multilined RTL languages where other than TextComponents

843810 Newbie
Currently Being Moderated
Hi;
I am wondering about what have the Locale should do, in below fields:

1.Why multi-lined strings (Farsi language in my case) in GUI components such as JOptionPane doesn't automatically (without using setComponentOrientation or applyComponentOrientation) ordered as RTL?
for instance if the message of a showMessageDilog of JOptionPane is a multi-lined String, setDefaultLocale for this component will do nothing!
p.s: i know i can make each line a (J)Label and align it to the right...

2.Also components within a frame has the same issue as #1 (without setting component orientation and just by use of setDefaultLocale has no effect

3.Another subject is Numerics. Somewhere in my application i used this code snippet:
numf = NumberFormat.getInstance(new Locale("ar")); //arabic locale is vaileable...
((DecimalFormat)numf).applyPattern(".###");
numf.format(421.6584);
I'm aware of using NumericShaper however, but in docs for DecimalFormat we have:

"...It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits..."

and also in the table of its pattern specification, mentioned that Numbers are localized.

but it has no effect!

And another thing maybe useful to mention is: if the OS (here MSWindowsXP) set to locale Farsi (or Arabic) for its numbers, in AWT, without any special coding, every number will be in arabic format (as well as everything else not related to Java).
Although i can understand this last expression i mentioned, AWT relies on System Envirounment...my main questions are those numbered:
what Locale will do in these issues?

Edited by: j-lover on May 21, 2010 10:31 PM
  • 1. Re: trouble in multilined RTL languages where other than TextComponents
    843810 Newbie
    Currently Being Moderated
    Hi again,
    I have found the solution for one of these issues from [this page|http://www.java2s.com/Code/Java/Development-Class/FormattingMessagesArabicDigit.htm] .
    I post the example into here:
    import java.awt.event.*;
    import java.awt.*;
    import java.text.*;
    import javax.swing.*;
    
    public class ArabicDigits extends JPanel {
      static JFrame frame;
    
      public ArabicDigits() {
        NumberFormat nf = NumberFormat.getInstance();
        if (nf instanceof DecimalFormat) {
          DecimalFormat df = (DecimalFormat)nf;
          DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
    
    dfs.setDecimalSeparator('.'); // i just added this to illustrate what i claim
    
          // set the beginning of the range to Arabic or Farsi digits
          dfs.setZeroDigit('\u0660');// u0660 or 06F0
          
          df.setDecimalFormatSymbols(dfs);
        }
    
        // create a label with the formatted number
        Label label = new Label(nf.format(1234567.89));// originally it was JLabel
    
        // set the font with a large enough size so we can easily read the numbers
        label.setFont(new Font("Lucida Sans", Font.PLAIN, 22));
        add(label);
      }
    
      public static void main(String [] argv) {
        ArabicDigits panel = new ArabicDigits();
        frame = new JFrame("Arabic Digits");
        frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {System.exit(0);}});
        frame.getContentPane().add("Center", panel);
        frame.pack();
        frame.setVisible(true);
      }
    }
    But still there remains a little bit of problem:
    As you see by running the original program (you will see the result in that link, or just use "J" prefix to Label in program posted here), the decimal separator is same as for English locale, although I like that, because I wanted in my Farsi application to have the period character ( ‘.’ ) as the separator between decimal portion and fraction part, but in my case, that is a tooltip for TrayIcon, the period character automatically changes to Arabic locale ( ‘,’ ) even by setting Decimal Separator explicitly to ‘.’ (or its Unicode code point equivalent ‘\u002E’ ):

    So, I guessed that it should be the matter of AWT/Swing, I experimented and that was right. For example you can make a very little bit change in the first code example and see what I am saying: just use Label instead of JLabel ! ( i made that befor posting)
    However this is not a big problem, I can use “/” for separator and that’s more common in my locale, but I am curious that why setting explicitly the separator this way:
    dfs.setDecimalSeparator('.');
    doesn't do what it expected to do !?

    Any idea will be much appreciated...

    Sincerely