Java Swing Tutorials - Herong's Tutorial Examples - Version 4.11, by Dr. Herong Yang
Editing Unicode Characters in an Editor Pane
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) 2014, HerongYang.com, All Rights Reserved. */ 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:
Sample programs listed in this section have been tested with JDK 1.6.0 to 1.8.0.
Last update: 2014.
Table of Contents
Introduction of Java Swing Package
Graphics Environment of the Local System
JRadioButton - Swing Radio Button Class
JTextField - Swing Text Field Class
Menu Bar, Menus, Menu Items and Listeners
Creating Internal Frames inside the Main Frame
Layout of Components in a Container
►JEditorPane - The Editor Pane Class
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