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:

Main.java

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();

        frame.setContentPane(contentPane);

        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        contentPane.setBorder(BorderFactory.createEmptyBorder(100,100,100,100));

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

        //this sets the text to what the user would enter
        inputTextBox.setText("\\u00C3");
        JButton showBtn = new JButton();

        showBtn.setText("Show");
        displayLbl.setText(" ");


        //add objects to JPanel
        contentPane.add(inputTextBox);
        contentPane.add(displayLbl);
        contentPane.add(showBtn);

        frame.pack();
        frame.setVisible(true);


        //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
                //showBtnActionPerformed
                showBtnActionPerformed(evt);
            }
        });


    } //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.
        //displayLbl.setText("\u00C3");


    } //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.

http://www.velocityreviews.com/FORUMS/t367758-unescaping-unicode-code-points-in-a-java-string.html

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
sb.append(c);
}
return sb.toString();
}
This question has already been answered. Start a new discussion instead.