Reading unicode from swing component and displaying it in swing component fails
I am trying to display a unicode string in a table as specified in the following program.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author dell
*/
public class UnicodeExample extends javax.swing.JFrame {
/** Creates new form UnicodeExample */
public UnicodeExample() {
initComponents();
setTitle("Unicode Example");
setSize(400, 400);
setLocationRelativeTo(null);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initCom开发者_开发百科ponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel1.setText("Enter :");
getContentPane().add(jLabel1);
jLabel1.setBounds(24, 50, 70, 20);
jTextField1.setText("\\u00a5");
getContentPane().add(jTextField1);
jTextField1.setBounds(110, 50, 220, 20);
jButton1.setText("Display Entered UniCode");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(120, 110, 160, 23);
getContentPane().add(jLabel2);
jLabel2.setBounds(120, 170, 150, 40);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
String uniCode = jTextField1.getText();
jLabel2.setText(uniCode);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UnicodeExample().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
But the Unicode characters are not displaying. This is happening only when I get the value from an externel source as a parameter and set that value to the Swing control. But there is no problem if I hard code the string like String unicode="\u00a5"
Is there any fix to this problem ?
Hard coding works because \u00a5
is considered as one single unicode character in that case.
However, when getting from the JTextField
, you are getting the STRING "\u00a5"
which is equivalent to "\\u00a5"
if you hard code.
To be more clear, try
char c = '\u00a5';
System.out.println(c);
Edit:
If you only insert unicode representations in the input box, this little code may help:
String uniCode = jTextField1.getText();
uniCode = uniCode.substring(2);
char c = (char) Integer.parseInt(uniCode, 16);
jLabel2.setText(c + "");
Do this in the actionPerformed
method.
精彩评论