4

How can I display a single Urdu character in a JTextPane? I have translated English characters to Urdu characters. But I can't find any way to correctly display those characters into my text component.

My goal is to:

  1. Get the key pressed on the keyboard.
  2. Convert that key into the equivalent Urdu character.
  3. Display it in my text component (JTextPane).

I've completed step 1 and 2 but can't work out the last one.

highBandWidth
  • 16,751
  • 20
  • 84
  • 131
Muhammad Salman Farooq
  • 1,325
  • 9
  • 26
  • 54
  • Try setting the font of your `JTextPane` to `Font.DIALOG`. Does it change anything? – Laf Nov 29 '11 at 20:12
  • i have done with setText() method, but my requirement is to set the text in the component when ever the key is pressed, i mean key pressed, translated, displayed. Thanks. – Muhammad Salman Farooq Nov 29 '11 at 20:14
  • 1
    If I recall correctly, such problems may come from the fact that the font used does not have a glyph to represent the character you wish to display. If you are using a font that cannot display urdu characters, it will not work. My previous comment was erroneous, as I don't think the `Font.DIALOG` font supports such characters. However, if you try to set a font that supports it on the `JTextPane`, does it fix your problem? What font is set on that object at the moment? – Laf Nov 29 '11 at 20:23
  • 2
    Found this other [question](http://stackoverflow.com/questions/6214829/english-characters-dont-show-up-when-entering-text-with-urdu-fonts-in-swing), maybe it will help you. – Laf Nov 29 '11 at 20:30

1 Answers1

5

to 3- display it in my text component that is JTextPane

enter image description here

source Wikipedia

project Encoded in plain UTF-8

import javax.swing.*;
import java.awt.*;

public class Example {

    private JFrame frameA = new JFrame("Example");
    private JTextArea textA = new JTextArea(10, 5);

    public Example() {
        frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        textA.setForeground(new Color(255, 150, 150));
        textA.setText("Peace be upon you (Hello) - السلام علیکم " + "\n");
        textA.append("Peace be upon you too (Hello) - و علیکم السلام " + "\n");
        textA.append("I am happy to meet you - آپ سے مل کر خوشی ہوئی" + "\n");
        textA.append("Do you speak English? - کیا آپ انگریزی بولتے ہیں؟" + "\n");

        frameA.add(textA);
        frameA.pack();
        frameA.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Example exam = new Example();
            }
        });
    }
} 

EDIT:

thanks Stas

by mistake I put that to the JTextArea

added JTextPane example

import javax.swing.*;
import java.awt.*;

public class Example {

    private JFrame frameA = new JFrame("Example");
    private JTextPane textP = new JTextPane();

    public Example() {
        frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        textP.setForeground(new Color(255, 150, 150));
        textP.setText("Peace be upon you (Hello) - السلام علیکم " + "\n"
        +"Peace be upon you too (Hello) - و علیکم السلام " + "\n"
        +"I am happy to meet you - آپ سے مل کر خوشی ہوئی" + "\n"
        +"Do you speak English? - کیا آپ انگریزی بولتے ہیں؟" + "\n");

        frameA.add(textP);
        frameA.pack();
        frameA.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Example exam = new Example();
            }
        });
    }
} 
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Your example about JTextArea not JTextPane. – StanislavL Nov 30 '11 at 05:49
  • Thanks for your help , but I aleardy know the setText method. Actually i am developing an urdu editor, i have successfully displayed my characters into the text component using the setText() method, but when i edit some text in it, it takes the carret to the last position and add the text at the end of the last entered word. Means one cannot edit any text in between the existing text. So Thats why I thought if there is any way to display in textPane character by character so that i can edit the text as well. any help in this regard. Thanks. – Muhammad Salman Farooq Nov 30 '11 at 17:29
  • @Muhammad Salman Farooq can you edit your post, put there some text in your language, hmmmm are you ComponentOrientation.RIGHT_TO_LEFT rellated ???, then to check this tutorial http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html – mKorbel Nov 30 '11 at 17:35