[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

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);
   }
}
0
zolf
Asked:
zolf
  • 10
  • 5
  • 3
  • +2
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 10
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now