1,讓文本域可以undo,比如支援ctrl+z,ctrl+y,ctrl+s(聯想windows的notepad)
下面是一個可以undo的文本域:

package com.swing.component;
import java.awt.event.actionevent;
import javax.swing.abstractaction;
import javax.swing.jtextarea;
import javax.swing.keystroke;
import javax.swing.event.undoableeditevent;
import javax.swing.event.undoableeditlistener;
import javax.swing.text.document;
import javax.swing.undo.cannotredoexception;
import javax.swing.undo.cannotundoexception;
import javax.swing.undo.undomanager;
public class undotextarea extends jtextarea {
private static final long serialversionuid = 2622113838910292609l;
private undomanager undo = new undomanager();
private document doc = getdocument();
private stringbuffer stringbuf = null;
public void stopundo() {
// undo.die();
undo.discardalledits();
}
public undomanager getundo() {
return undo;
public void setundo(undomanager undo) {
this.undo = undo;
public document getdoc() {
return doc;
public void setdoc(document doc) {
this.doc = doc;
/**
* @autho : whuang dicate original content of the area
* @return
*/
public stringbuffer getstringbuf() {
return stringbuf;
/***
* dicate original content of the area
*
* @param stringbuf
public void setstringbuf(stringbuffer stringbuf) {
this.stringbuf = stringbuf;
private void initlize() {
doc.addundoableeditlistener(new undoableeditlistener() {
public void undoableedithappened(undoableeditevent e) {
undo.addedit(e.getedit());
}
});
addactionmap();
public undotextarea() {
initlize();
public undotextarea(int rows, int columns) {
super(rows, columns);
public void addactionmap() {
getactionmap().put("undo", new abstractaction("undo11") {
private static final long serialversionuid = 2434402629308759912l;
public void actionperformed(actionevent evt) {
try {
boolean b = undo.canundo();
// system.out.println("whether undo : "+b);
if (b) {
undo.undo();
}
} catch (cannotundoexception e) {
}
getinputmap().put(keystroke.getkeystroke("control z"), "undo");
getactionmap().put("redo", new abstractaction("redo1111") {
private static final long serialversionuid = 5348330289578410517l;
if (undo.canredo()) {
undo.redo();
} catch (cannotredoexception e) {
getinputmap().put(keystroke.getkeystroke("control r"), "redo");
getactionmap().put("copy", new abstractaction("copy111") {
private static final long serialversionuid = -5151480809625853288l;
copy();
getinputmap().put(keystroke.getkeystroke("control c"), "copy");
getactionmap().put("cut", new abstractaction("cut") {
private static final long serialversionuid = 7316612864835857713l;
cut();
getinputmap().put(keystroke.getkeystroke("control x"), "cut");
getactionmap().put("paste", new abstractaction("paste111") {
private static final long serialversionuid = -3548620001691220571l;
paste();
getinputmap().put(keystroke.getkeystroke("control v"), "paste");
// redo ctrl + y
getactionmap().put("redo", new abstractaction("redo111") {
if (undo.canredo()) {
undo.redo();
getinputmap().put(keystroke.getkeystroke("control y"), "redo");
}
示例:

inputtextarea = new undotextarea();
inputtextarea.setlinewrap(true);
inputtextarea.setwrapstyleword(true);
(2)增加右鍵菜單
效果如下:
代碼如下:

package com.swing.menu;
import java.awt.event.mouseevent;
import javax.swing.jmenuitem;
import javax.swing.jpopupmenu;
import javax.swing.jtextfield;
import javax.swing.event.mouseinputlistener;
import javax.swing.text.jtextcomponent;
public class menuutil2
{
public static final string action_str_open = "open";
public static final string action_str_delete_file = "delete file";
public static final string action_str_exit = "exit";
public static final string action_str_browser = "browser";
public static final string action_str_copy = "copy";
public static final string action_str_copy_all = "copy all";
public static final string action_str_paste = "paste";
public static final string action_str_replace_all = "replace all";
public static final string action_str_delete_content = "delete";
public static final string action_str_delete_all_content = "delete all";
public static final string action_str_select_all_content = "select all";
public static final string action_str_refresh = "refresh";
public static final string action_str_copy_filepath = "copy file path";
public static final string action_str_file_rename = "rename";
public static final string action_str_close = "close";
public static final string action_str_new = "new";
public static final string action_str_add = "add";
public static final string action_str_save = "save";
public static final string action_str_edit = "edit";
public static final string action_str_view = "view";
public static final string action_str_update = "update";
public static final string action_str_insert = "insert";
private menuutil2()
{
throw new error("don't let anyone instantiate this class.");
* 給文本框增加右鍵菜單.
* @param field2
public static void setpopupmenu(final jtextcomponent field2)
field2.addmouselistener(new mouseinputlistener()
{
@override
public void mousemoved(mouseevent e)
{
public void mousedragged(mouseevent e)
public void mousereleased(mouseevent e)
// super.mousepressed(e);
if (e.getbutton() == mouseevent.button3)
{
jpopupmenu textmenu = new jpopupmenu();
jmenuitem copym = new jmenuitem(menuutil2.action_str_copy);
jmenuitem copyallm = new jmenuitem(
menuutil2.action_str_copy_all);
jmenuitem pastem = new jmenuitem(menuutil2.action_str_paste);
jmenuitem replaceallm = new jmenuitem(menuutil2.action_str_replace_all);
jmenuitem deletem = new jmenuitem(
menuutil2.action_str_delete_content);
jmenuitem deleteallm = new jmenuitem(
menuutil2.action_str_delete_all_content);
jmenuitem selallm = new jmenuitem(
menuutil2.action_str_select_all_content);
menu2actionlistener mymenulistener=new menu2actionlistener(field2);
copym.addactionlistener(mymenulistener);
copyallm.addactionlistener(mymenulistener);
pastem.addactionlistener(mymenulistener);
replaceallm.addactionlistener(mymenulistener);
deletem.addactionlistener(mymenulistener);
deleteallm.addactionlistener(mymenulistener);
selallm.addactionlistener(mymenulistener);
textmenu.add(copym);
textmenu.add(copyallm);
if(!(field2 instanceof jtextfield)){
/*因為jtextfield 沒有 ta2.insert(content, caret) 方法 .*/
textmenu.add(pastem);
textmenu.add(replaceallm);
textmenu.add(deletem);
textmenu.add(deleteallm);
textmenu.add(selallm);
textmenu.show(e.getcomponent(), e.getx(), e.gety());
public void mousepressed(mouseevent e)
public void mouseexited(mouseevent e)
public void mouseentered(mouseevent e)
public void mouseclicked(mouseevent e)
import java.awt.event.actionlistener;
import com.common.util.windowutil;
/***
* 文本域的右鍵菜單響應事件.
*
* @author huangwei
*
*/
public class menu2actionlistener implements actionlistener {
private jtextcomponent area2;
public menu2actionlistener(jtextcomponent area2) {
super();
this.area2 = area2;
@override
public void actionperformed(actionevent event) {
string command = event.getactioncommand();
if (command.equals(menuutil2.action_str_delete_content)) {
// system.out.println("delete word");
area2.replaceselection("");
} else if (command.equals(menuutil2.action_str_delete_all_content)) {
area2.selectall();
} else if (command.equals(menuutil2.action_str_select_all_content)) {
if (area2 != null) {
area2.selectall();
// system.out.println("select all");
} else if (command.equals(menuutil2.action_str_copy)) {
string selectcontent = area2.getselectedtext();
if (selectcontent == null || selectcontent.equals("")) {
return;
windowutil.setsysclipboardtext(selectcontent);
} else if (command.equals(menuutil2.action_str_copy_all)) {
string selectcontent = area2.gettext();
} else if (command.equals(menuutil2.action_str_paste)) {
string content = windowutil.getsysclipboardtext();
if (content == null || content.equals("")) {
int caret = area2.getcaretposition();
if(area2 instanceof jtextarea){
jtextarea ta2=(jtextarea)area2;
ta2.insert(content, caret);
} else if (command.equals(menuutil2.action_str_replace_all)) {
area2.settext(content);
}
}