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
}