Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UTF-8 and JTextArea

Tags:

java

i have 2 JTextArea that one of these contain Unicode Code point like this \u0645 i want another JTextArea

show Character representation of this Unicode code point.but when pass this code point to JTextArea , it show

code point not Character but if i set code point to JTextArea setText method directly it work correctly !

why ? and which can i pass String of Codepoint from one JTextArea to another ?

thanks

like image 243
mehdi shahdoost Avatar asked Mar 31 '26 18:03

mehdi shahdoost


1 Answers

This code displays a character, and in the other text-area the corresponding "unicode string" counterpart:

import java.awt.*;

import javax.swing.*;
public class FrameTest {
    public static void main(String[] args) {
        JFrame jf = new JFrame("Demo");
        Container cp = jf.getContentPane();
        cp.setLayout(new BorderLayout());
        JTextArea ta1 = new JTextArea(20, 20);
        JTextArea ta2 = new JTextArea(20, 20);
        Character c = '\u0645';
        ta1.setText("" + c);
        String s = String.format("\\%04x", (int) c.charValue());
        ta2.setText(s);
        cp.add(ta1, BorderLayout.WEST);
        cp.add(ta2, BorderLayout.EAST);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(500, 100);
        jf.setVisible(true);
    }
}

enter image description here


So if you have a long text of such characters, you would need to loop through the string character by character, (using getCharAt(int) or getChars()) and process each character with String.format("\\%04x", (int) c.charValue()); and append the result to the target string. (Preferrably using a StringBuffer.)

like image 116
aioobe Avatar answered Apr 03 '26 09:04

aioobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!