I am trying to write a program to display the unicode character when a user enters a unicode character code.

ex: If the user enters: "\u00C3" in the textfield, I want to display a capital "A" with a tilda (~) over the top of it. Here's my code so far:


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

public class Main {

    private static JTextField inputTextBox;
    private static JLabel displayLbl;

    public static void main(String s[]) {

        //create new frame
        JFrame frame = new JFrame("Unicode Characters");

        //add window listener
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}

        //create new JPanel
        JPanel contentPane = new JPanel();


        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        inputTextBox = new JTextField(JTextField.CENTER);
        displayLbl = new JLabel();

        //this sets the text to what the user would enter
        JButton showBtn = new JButton();

        displayLbl.setText(" ");

        //add objects to JPanel


        //add listener for button
        showBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                //do something, in this case we call method

    } //main

  private static void showBtnActionPerformed (java.awt.event.ActionEvent evt){
        String userText;
        char myUnicodeChar;

        //get user inputed unicode character ex: \u00C3
        userText = inputTextBox.getText();

        //display character for unicode code
        displayLbl.setText("" + userText);

        //the line below displays what I want to display
        //but I want the user to be able to enter "\u00C3"
        //and then I want to display the character that is
        //represented by it.

    } //showBtnActionPerformed


Edited 7 Years Ago by cgeier: n/a

Thanks for the reply. I'm trying to convert from a String (user entered value in a TextField) to a unicode character. I'm having trouble seeing how to get it from a String to a unicode character.

Edited 7 Years Ago by cgeier: n/a

I was able to find a solution on another site.


Here is the solution:

private static String unescape(String s) {
int i=0,len=s.length(); char c; StringBuffer sb = new StringBuffer(len);
while (i<len) {
     c = s.charAt(i++);
     if (c=='\\') {
          if (i<len) {
               c = s.charAt(i++);
               if (c=='u') {
                    c = (char) Integer.parseInt(s.substring(i,i+4),16);
                    i += 4;
               } // add other cases here as desired...
     } // fall through: \ escapes itself, quotes any character but u
return sb.toString();
This question has already been answered. Start a new discussion instead.