Click here to Skip to main content
13,254,366 members (58,688 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 3 Dec 2015

Simple texteditor with Java Swing

, 3 Dec 2015
Rate this:
Please Sign up or sign in to vote.
A usecase for Java beginner

Using Java to Build a Simple Text Editor Application

  • Create menu, contextual menu
  • Using toolbox component
  • Using JTextPane
  • Understand JFileChooser, JColorChooser


  • Basic Java UI component
  • Read/write file


Load all system font on font combobox:

private void loadFont() {
GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
// get all font name 
String[] fontNames = gEnv.getAvailableFontFamilyNames();
// load to combobox
ComboBoxModel model = new DefaultComboBoxModel(fontNames);

When user selects font and size, we will be setting font and size for textpane component:

private void jcbFontActionPerformed(java.awt.event.ActionEvent evt) {
// Change font of text
jTextPane1.setFont(new Font(jcbFont.getSelectedItem().toString(),
Font.PLAIN, Integer.parseInt(jcbSelectSize.getSelectedItem().toString())));
private void jcbSelectSizeActionPerformed(java.awt.event.ActionEvent evt) {
// Select size of text
String getSize = jcbSelectSize.getSelectedItem().toString();
Font f = jTextPane1.getFont();
// setting new size
jTextPane1.setFont(new Font(f.getFontName(),
f.getStyle(), Integer.parseInt(getSize)));

JColorChooser API of Java swing helps us get a color from system color dialog, using code below for ActionPerformed event of color button.

private void btnSelectColorActionPerformed(java.awt.event.ActionEvent evt) {
Color jColor = selectColor;
// open color dialog and select Color
if ((jColor = JColorChooser.showDialog(this, "Select color", jColor)) != null) {
selectColor = jColor;
// set text color

RTFEditorKit Can Help to Reading Formatted Text on JTextPane and Write Down File System with Rich Text Format

When user clicks on Save button or save as menu:

private void save() {
JFileChooser file = new JFileChooser();
TextFilter filter = new TextFilter();
String fileName = "";
// show save file dialog
if (file.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
// get full path of selected file
fileName = file.getSelectedFile().getAbsolutePath();
// get meta of text
StyledDocument doc = (StyledDocument) jTextPane1.getDocument();
// convert to richtext format
RTFEditorKit kit = new RTFEditorKit();
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(fileName));
// save content to file
kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());
} catch (Exception e) {
System.out.println("Err:" + e.toString());
} else {

Handing button Open file or menu Open file as:

private void open() {
JFileChooser file = new JFileChooser();
TextFilter filter = new TextFilter();
String fileName = "";
// show open file dialog
if (file.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
fileName = file.getSelectedFile().getAbsolutePath();
} else {
// using richtext format
RTFEditorKit rtf = new RTFEditorKit();
try {
// load file into jTextPane
FileInputStream fi = new FileInputStream(fileName);, jTextPane1.getDocument(), 0);
} catch (Exception e) {
System.out.println("err:" + e.toString());

Points of Interest

This is just demo Java beginner.

Maybe, see step by step video ==>> here <<==


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ngo Tuong Dan
Instructor / Trainer Can Tho University Software Center
Vietnam Vietnam
Code for fun -> ^.^ <-

You may also be interested in...

Comments and Discussions

QuestionMy vote of 5 Pin
Mohammad Shuvo24-Oct-16 7:30
memberMohammad Shuvo24-Oct-16 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 4 Dec 2015
Article Copyright 2015 by Ngo Tuong Dan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid