1 June 2011

UnDo/ReDo in JTextArea Tutorial

UnDo/ReDo in JTextArea Tutorial
Hello Friends,

Nice to see you again.
In Previous Sessions  we have seen lots of  Java Swing Components Demo. 
And learn how they work.

Today, we will see the example of  enabling the Undo/Redo Capacity in JTextArea component of Java Swing.

Normally if you see in any operating system, the Undo, Redo functionality is given in Text Components.
So based on this concept, If we want to develop Java Swing Application which also provide Undo, Redo
facility for its Text Component than what will we do..?

As there is no such direct method which provide you this facility at once.
So we have to write some sort of code to enable this undo-redo facility.

It is very easy to understand the code for undo-redo functionality in JTextArea or JTextField.

So be ready to learn this simple exmaple of Undo-Redo in JTextArea.
After apply this code in action, you will be capable of using Undo-Redo in your Swing Applications.

Note: This code is working in Window and Mac. Operating System. 
          But I am not sure that it runs in other OS or not. So Just check 
          it and tell me.

Output will be like this:













Note: Here I have applied this code on JTextArea. But if you want to enable Undo-Redo in JTextField than please only replace JTextField in place of JTextArea. And run the code.


OK For this you have to create one class called UndoRedoTextApp.java and paste the below code in it.
And run it....Simple right..?


----------------------------------------------------------------------------------------------

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.undo.*;
import javax.swing.event.*;

public class UndoRedoTextApp extends JFrame {

public UndoRedoTextApp() {
super("Undo/Redo Demo");

Font font = new Font("", 1, 18);

JLabel label = new JLabel("Type Something And Press Ctrl+z" +
" or Ctrl+y For Undo & Redo");
label.setForeground(Color.RED);
label.setFont(font);
getContentPane().add(label, BorderLayout.NORTH);

// Replace Below Two lines of JTextArea with
// JTextField textcomp = new JTextField();
// textcomp.setPreferredSize(new Dimension(50, 50));
//it will change JTextArea with JTextField
JTextArea textcomp = new JTextArea();
textcomp.setPreferredSize(new Dimension(600,300));
final UndoManager undo = new UndoManager();
Document doc = textcomp.getDocument();

// Listen for undo and redo events
doc.addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent evt) {
undo.addEdit(evt.getEdit());
}
});

// Create an undo action and add it to the text component
textcomp.getActionMap().put("Undo", new AbstractAction("Undo") {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canUndo()) {
undo.undo();
}
} catch (CannotUndoException e) {
}
}
});

// Bind the undo action to ctl-Z (or command-Z on mac)
textcomp.getInputMap().put(
KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit
.getDefaultToolkit().getMenuShortcutKeyMask()), "Undo");

// Create a redo action and add it to the text component
textcomp.getActionMap().put("Redo", new AbstractAction("Redo") {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canRedo()) {
undo.redo();
}
} catch (CannotRedoException e) {
}
}
});

// Bind the redo action to ctl-Y (or command-Y on mac)
textcomp.getInputMap().put(
KeyStroke.getKeyStroke(KeyEvent.VK_Y, 
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), 
"Redo");

getContentPane().add(textcomp, BorderLayout.CENTER);

pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String argv[]) {
new UndoRedoTextApp();
}
}
--------------------------------------------------------------------

So I hope this code is helpful in understanding the working of Undo-Redo
functionality in JTextArea & JTextField.

Feel free to ask me if you have any dought regarding this.
You can also give me suggessions to improve this tutorial.

Leave comments if you like this post...!

Thank you.
Nirav Raval

3 comments:

  1. Its really great, i have visited many sites, but the best working code is here... Thanks

    ReplyDelete
  2. it's really helped me a lot thank you very much

    ReplyDelete

Leave Comment If You Like This...Thank You..!