Java入力ボックスundo


クラスUndoTextArea:
package com.pass.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;
    UndoManager undo = new UndoManager();
    Document    doc  = getDocument();

    public void stopUndo()
    {
//        undo.die();
        undo.discardAllEdits();
    }

    public UndoTextArea()
    {
        doc.addUndoableEditListener(new UndoableEditListener()
        {
            public void undoableEditHappened(UndoableEditEvent e)
            {
                undo.addEdit(e.getEdit());
            }
        });
        addActionMap();
    }

    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;

            public void actionPerformed(ActionEvent evt)
            {
                try
                {
                    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;

            public void actionPerformed(ActionEvent evt)
            {
                copy();
            }

        });
        getInputMap().put(KeyStroke.getKeyStroke("control C"), "Copy");

        getActionMap().put("Cut", new AbstractAction("Cut")
        {

            private static final long serialVersionUID = 7316612864835857713L;

            public void actionPerformed(ActionEvent evt)
            {
                cut();
            }

        });

        getInputMap().put(KeyStroke.getKeyStroke("control X"), "Cut");

        getActionMap().put("Paste", new AbstractAction("Paste111")
        {
            private static final long serialVersionUID = -3548620001691220571L;

            public void actionPerformed(ActionEvent evt)
            {
                paste();
            }
        });

        getInputMap().put(KeyStroke.getKeyStroke("control V"), "Paste");
    }

}
 
テストクラス:FrameMain
package com.pass.test;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.pass.component.UndoTextArea;

public class FrameMain extends JFrame
{
    private static final long serialVersionUID = 3526577815017461757L;
    public static void main(String[] args)
    {
        new FrameMain().launchFrame();
    }
    public void launchFrame(){
        JPanel pane=new JPanel();
        pane.setLayout(new GridLayout(2, 1));
        UndoTextArea txt1= new UndoTextArea();
        txt1.setBackground(new Color(255, 255, 100));
        txt1.setText("txt1");
        txt1.stopUndo();
        UndoTextArea txt2= new UndoTextArea();
        txt2.setBackground(new Color(255, 100, 100));
        txt2.setText("txt2");
        txt2.stopUndo();
        pane.add(txt1);
        pane.add(txt2);
        this.getContentPane().add(pane);
        this.setBounds(100, 100, 400, 400);
        this.setDefaultCloseOperation(3);
        this.setVisible(true);
    }

}
 
FrameMainを実行し、swingウィンドウをポップアップ
どんな内容を入力しても、Ctrl+Zでテストできます.