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

}

Recommended Answers

All 3 Replies

Character ch = new Character('\u00C3');
char prim = ch.charValue();
System.out.println(prim);

Something I just learned how to do after a quick look up in the Character class documentation. I'm sure if you read some stuff yourself and try some things out, you'll find many more useful methods. http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html

commented: Good pointer +11

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.

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();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.