Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

Editing Unicode Characters in an Editor Pane

This section provides a tutorial example on how to handle Chinese and other language characters as Unicode characters in the editor pane. UTF-8 encoding should be used to write and read Unicode characters to and from files.

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:
Editor Pane Test

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.

Last update: 2009.

Sections in This Chapter

Creating a Simple Plain Text Editor Pane

Saving Text from an Editor Pane

Editing HTML in an Editor Pane

Editing Unicode Characters in an Editor Pane

javax.swing.JFileChooser - File Chooser Dialog Box

Dr. Herong Yang, updated in 2009
Editing Unicode Characters in an Editor Pane