Keyboard Language Setting in Swing Applications


 in a swing application to input a foreign language string like
persian i have to change keyboard language manually by pressig alt + shift
in windows os.

im looking a way to change this setting automatically to all input field that must
accept persian language.

this is my code below.but it does not type in persian.please helppppp

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Locale;
import java.util.*;
import javax.swing.JComponent;

public class JTArea extends JFrame {
 
   public JTArea() {
     super("?????");
       String message = null;
     Font font = new Font("Arial", Font.PLAIN, 20);
     
     Locale l = new Locale("fa");
     Locale.setDefault(l);

     System.out.println(Locale.getDefault());
     
     JTextArea textArea = new JTextArea(l.getDisplayLanguage());
     textArea.setDefaultLocale(l);
     textArea.setFont(font);
     this.getContentPane().add(textArea);
     textArea.setVisible(true);
   }
 
   public static void main(String[] args) {
      String fntName="Arial";
     JFrame frame = new JTArea();
   frame.setFont(new Font(fntName,Font.PLAIN,11));
   
   
   
     frame.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {System.exit(0);}
     });
     frame.pack();
     frame.setVisible(true);
   }
}
zolfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
>> Locale.setDefault(l);

Why do you do that? You can just set the locale for the text-areas using textArea.setDefaultLocale () I guess.
0
zzynxSoftware engineerCommented:
The Locale class has this constructors:

Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String variant)
0
CEHJCommented:
public static void main(String[] args) {
    JComponent.setDefaultLocale(new Locale("fa"));
    ...
}
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

zolfAuthor Commented:

textArea.setDefaultLocale ()

i have done that too,but does not work.you can run the code to see for yourself

please help
0
Mayank SAssociate Director - Product EngineeringCommented:
Tried it without doing Locale.setDefault () ?
0
zolfAuthor Commented:

yes i tried
0
objectsCommented:
try setting the font to something that supports the character set you are using
0
zolfAuthor Commented:

arial font is supported.Because when i change the language in windows xp(pressing alt+shift)to persian or arabic and then type in the text box it types in that language.
0
zzynxSoftware engineerCommented:
zolf, have you already tried CEHJ's suggestion?
0
zolfAuthor Commented:

yes i tried CEHJ's suggestion, but did not help.
0
objectsCommented:
Is your JRE configured to support that Locale?
do you have the file:

<jre>/lib/font.properties.fa
0
zolfAuthor Commented:

i dont have it.but arabic is supported by java.i mean when i run this code,arabic (ar) is available.

Locale list[] = DateFormat.getAvailableLocales();
      for (int i = 0; i < list.length; i++) {
          System.out.println(list[i].toString());
      }
0
CEHJCommented:
What are the symptoms of the problem? Is it showing incorrect characters, or what?
0
objectsCommented:
try setting the input method for the window

System.out.println("selectInputMethod: "+frame.getInputContext().selectInputMethod(locale));

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zolfAuthor Commented:

no it types properly in the textfield.but i have to change the language in windows xp(pressing alt+shift) to persian or arabic to be able to type in those language respectively.i want to be able to type without pressing alt+shift
0
zolfAuthor Commented:

objects where do i paste this code in the constructor or main method
0
zolfAuthor Commented:

object this is what i did in main i have shown it between the comment
public static void main(String[] args)
   {
               JComponent.setDefaultLocale(new Locale("ar"));
               
               
            String fntName="Arial";
           JFrame frame = new JTArea();
               frame.setFont(new Font(fntName,Font.PLAIN,11));
               
               //added coded
               Locale l = new Locale("ar");
            System.out.println("selectInputMethod: "+frame.getInputContext().selectInputMethod(l));
            //end code
            
           frame.addWindowListener(new WindowAdapter() {
       
        public void windowClosing(WindowEvent e) {System.exit(0);}
           });
           
           frame.pack();
           frame.setVisible(true);
   }
0
zolfAuthor Commented:

it gives me false
0
zolfAuthor Commented:

thanks mate.it worked.in the constructor of Locale i used the second constructor i.e Locale l = new Locale("fa","IR");

cheers
zolf
0
objectsCommented:
Maybe that you cannot avoid doing it via the OS.
I'll ask around and see if there is a way.
0
objectsCommented:
excellent, had me scratching my head there for a minute :)
0
zzynxSoftware engineerCommented:
>> it worked.in the constructor of Locale i used the second constructor
And what did I told you in my first comment?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.