Display unicode character

Thread Solved

Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster

Display unicode character

 
0
  #1
Oct 21st, 2009
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
1
  #2
Oct 21st, 2009
  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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster
 
0
  #3
Oct 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster
 
0
  #4
Oct 25th, 2009
I was able to find a solution on another site.

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

Here is the solution:
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC