Mapping Urdu Characters in JTextPane

How can I display a single Urdu character in a JTextPane ? I translated English characters into Urdu. But I cannot find a way to correctly display these characters in my text component.

My goal:

  • Get the key pressed on the keyboard.
  • Convert this key to an equivalent Urdu symbol.
  • Show it in a text component ( JTextPane ).

I followed steps 1 and 2, but cannot solve the last one.

+4
source share
2 answers

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

enter image description here

Wikipedia source

Project Encoded in Simple 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 this on 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(); } }); } } 
+5
source

This is an old question, but if someone is still looking for an answer, I just wrote a blog. You can go and read here

0
source

Source: https://habr.com/ru/post/1383719/


All Articles