944,011 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2477
  • Java RSS
Oct 21st, 2009
0

Display unicode character

Expand Post »
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
Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3.  
  4. public class Main {
  5.  
  6. private static JTextField inputTextBox;
  7. private static JLabel displayLbl;
  8.  
  9. public static void main(String s[]) {
  10.  
  11. //create new frame
  12. JFrame frame = new JFrame("Unicode Characters");
  13.  
  14.  
  15. //add window listener
  16. frame.addWindowListener(new WindowAdapter() {
  17. public void windowClosing(WindowEvent e) {System.exit(0);}
  18. });
  19.  
  20. //create new JPanel
  21. JPanel contentPane = new JPanel();
  22.  
  23. frame.setContentPane(contentPane);
  24.  
  25. contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
  26. contentPane.setBorder(BorderFactory.createEmptyBorder(100,100,100,100));
  27.  
  28. inputTextBox = new JTextField(JTextField.CENTER);
  29. displayLbl = new JLabel();
  30.  
  31. //this sets the text to what the user would enter
  32. inputTextBox.setText("\\u00C3");
  33. JButton showBtn = new JButton();
  34.  
  35. showBtn.setText("Show");
  36. displayLbl.setText(" ");
  37.  
  38.  
  39. //add objects to JPanel
  40. contentPane.add(inputTextBox);
  41. contentPane.add(displayLbl);
  42. contentPane.add(showBtn);
  43.  
  44. frame.pack();
  45. frame.setVisible(true);
  46.  
  47.  
  48. //add listener for button
  49. showBtn.addActionListener(new java.awt.event.ActionListener() {
  50. public void actionPerformed(java.awt.event.ActionEvent evt) {
  51. //do something, in this case we call method
  52. //showBtnActionPerformed
  53. showBtnActionPerformed(evt);
  54. }
  55. });
  56.  
  57.  
  58. } //main
  59.  
  60.  
  61. private static void showBtnActionPerformed (java.awt.event.ActionEvent evt){
  62. String userText;
  63. char myUnicodeChar;
  64.  
  65. //get user inputed unicode character ex: \u00C3
  66. userText = inputTextBox.getText();
  67.  
  68. //display character for unicode code
  69. displayLbl.setText("" + userText);
  70.  
  71. //the line below displays what I want to display
  72. //but I want the user to be able to enter "\u00C3"
  73. //and then I want to display the character that is
  74. //represented by it.
  75. //displayLbl.setText("\u00C3");
  76.  
  77.  
  78. } //showBtnActionPerformed
  79.  
  80. }
Last edited by cgeier; Oct 21st, 2009 at 6:05 pm.
Similar Threads
Reputation Points: 41
Solved Threads: 15
Junior Poster
cgeier is offline Offline
104 posts
since Oct 2008
Oct 21st, 2009
1
Re: Display unicode character
Java Syntax (Toggle Plain Text)
  1. Character ch = new Character('\u00C3');
  2. char prim = ch.charValue();
  3. 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/...Character.html
Last edited by BestJewSinceJC; Oct 21st, 2009 at 11:37 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 24th, 2009
0
Re: Display unicode character
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.
Last edited by cgeier; Oct 24th, 2009 at 12:15 pm.
Reputation Points: 41
Solved Threads: 15
Junior Poster
cgeier is offline Offline
104 posts
since Oct 2008
Oct 25th, 2009
0
Re: Display unicode character
I was able to find a solution on another site.

http://www.velocityreviews.com/FORUM...va-string.html

Here is the solution:
Java Syntax (Toggle Plain Text)
  1. private static String unescape(String s) {
  2. int i=0,len=s.length(); char c; StringBuffer sb = new StringBuffer(len);
  3. while (i<len) {
  4. c = s.charAt(i++);
  5. if (c=='\\') {
  6. if (i<len) {
  7. c = s.charAt(i++);
  8. if (c=='u') {
  9. c = (char) Integer.parseInt(s.substring(i,i+4),16);
  10. i += 4;
  11. } // add other cases here as desired...
  12. }
  13. } // fall through: \ escapes itself, quotes any character but u
  14. sb.append(c);
  15. }
  16. return sb.toString();
  17. }
Reputation Points: 41
Solved Threads: 15
Junior Poster
cgeier is offline Offline
104 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Another iText question...
Next Thread in Java Forum Timeline: Urgent! I need to teach someone 'special'





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC