This section provides a tutorial example on how to use ALTER TABLE statements to add, delete, modify, columns and indexes.
If you are trying to edit Chinese characters or other non-ASCII characters,
you must change the write function and the read function to support Unicode characters
with the UTF-8 encoding.
/**
* JEditorPaneUnicode.java
* Copyright (c) 2009 by Dr. Herong Yang, http://www.herongyang.com/
*/
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.awt.event.*;
import javax.swing.*;
public class JEditorPaneUnicode implements ActionListener {
JFrame myFrame = null;
JEditorPane myPane = null;
public static void main(String[] a) {
(new JEditorPaneUnicode()).test();
}
private void test() {
myFrame = new JFrame("JEditorPane Unicode Test");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(300,200);
myPane = new JEditorPane();
myPane.setContentType("text/plain");
myPane.setText(
"Hello computer! - \u7535\u8111\u4F60\u597D\uFF01\n"
+ "Welcome to Herong's Website!\n"
+ "\u6B22\u8FCE\u4F60\u8BBF\u95EE\u548C\u8363\u7F51\u7AD9"
+ "\uFF01\nwww.herongyang.com");
myFrame.setContentPane(myPane);
JMenuBar myBar = new JMenuBar();
JMenu myMenu = getFileMenu();
myBar.add(myMenu);
myFrame.setJMenuBar(myBar);
myFrame.setVisible(true);
}
private JMenu getFileMenu() {
JMenu myMenu = new JMenu("File");
JMenuItem myItem = new JMenuItem("Open");
myItem.addActionListener(this);
myMenu.add(myItem);
myItem = new JMenuItem("Save");
myItem.addActionListener(this);
myMenu.add(myItem);
return myMenu;
}
public void actionPerformed(ActionEvent e) {
String cmd = ((AbstractButton) e.getSource()).getText();
try {
if (cmd.equals("Open")) {
FileInputStream fis =
new FileInputStream("JEditorPane.txt");
InputStreamReader in =
new InputStreamReader(fis, Charset.forName("UTF-8"));
char[] buffer = new char[1024];
int n = in.read(buffer);
String text = new String(buffer, 0, n);
myPane.setText(text);
in.close();
} else if (cmd.equals("Save")) {
FileOutputStream fos =
new FileOutputStream("JEditorPane.txt");
OutputStreamWriter out =
new OutputStreamWriter(fos, Charset.forName("UTF-8"));
out.write(myPane.getText());
out.close();
}
} catch (Exception f) {
f.printStackTrace();
}
}
}
If you run this example, you will see a text editor pane displayed
with the initial text content including some Chinese characters.
You can enter more Chinese or other language characters,
click the Save link in the File menu, Chinese characters will be saved correctly,
see the picture below:
Interesting notes about this tutorial example:
I have to use OutputStreamWriter instead of FileWriter to save Unicode characters
to a file. OutputStreamWriter allows me to use the UTF-8 encoding.
I have to use InputStreamReader instead of FileReader to read Unicode characters
from a file. OutputStreamWriter allows me to use the UTF-8 encoding.
Sample programs listed in this section have been tested with JDK 1.6.0.