Java – Simple TextEditor

Using java to building a simple text editor application

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

You can down load source =>> here <<==

Video step by step ==>> at here <<==

Some app’s interface


Load all system font on font combobox

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

When user select font and size, we will 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 help our 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 reading formatted text on JTextPane and write down file system with rich text format

When user click 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());

This is just demo java beginner.
I hope that, it is useful for you.


Phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập: Logo

Bạn đang bình luận bằng tài khoản Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s