How to realize Undo, Redo,Copy, Paste and Cut with JAVA
Editor to share with you how to use JAVA to achieve Undo, Redo,Copy, Paste, Cut, I hope you will learn something after reading this article, let's discuss it together!
Package clipborad
Import Javax.swing.JTextArea
Import java.awt.datatransfer.*
Import java.awt.*
Import javax.swing.*
Import java.io.*
Import javax.swing.undo.*
Import javax.swing.text.*
Import javax.swing.event.*
Import java.awt.event.*
/ * *
* Title: implement Undo, Redo,Copy, Paste and Cut with JAVA
* Description: implement Undo, Redo,Copy, Paste and Cut with JAVA
* Copyright: Copyright (c) 2001
* Company:
* @ author: Peng Jianxiong
* @ version 1.0
, /
Public class ClipTextArea extends JTextArea implements ClipboardOwner {
Clipboard strBoard = this.getToolkit () .getSystemClipboard ()
Undomanager undo = new UndoManager ()
Document doc = getDocument ()
Public ClipTextArea () {
Doc.addUndoableEditListener (new UndoableEditListener () {
Public void undoableEditHappened (UndoableEditEvent e) {
Doc_addedit (e);}
});
AddActionMap ()
}
Public void addActionMap ()
{
GetActionMap () .put ("Undo", new AbstractAction ("Undo") {
Public void actionPerformed (ActionEvent evt) {
Try {
If (undo.canUndo ()) {
Undo.undo ()
}
} catch (CannotUndoException e) {
}
}
});
GetInputMap () .put (KeyStroke.getKeyStroke ("control Z"), "Undo")
GetActionMap () .put ("Redo", new AbstractAction ("Redo") {
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 ("Copy") {
Public void actionPerformed (ActionEvent evt) {
Copy ()
}
});
GetInputMap () .put (KeyStroke.getKeyStroke ("control C"), "Copy")
GetActionMap () .put ("Cut", new AbstractAction ("Cut") {
Public void actionPerformed (ActionEvent evt) {
Cut ()
}
});
GetInputMap () .put (KeyStroke.getKeyStroke ("control X"), "Cut"
GetActionMap () .put ("Paste", new AbstractAction ("Paste") {
Public void actionPerformed (ActionEvent evt) {
Paste ()
}
});
GetInputMap () .put (KeyStroke.getKeyStroke ("control V"), "Paste")
}
Public void copy ()
{
String SCOpy = getselectedText ()
If (sCopy! = null)
{
StringSelection sSelection = new StringSelection (sCopy)
StrBoard.setContents (sSelection,this)
}
}
Public void cut ()
{
String sCopy = getSelectedText ()
If (sCopy! = null)
{
StringSelection sSelection = new StringSelection (sCopy)
StrBoard.setContents (sSelection,this)
This.replaceSelection ("")
}
}
Public void paste ()
{
Transferable sTransf = strBoard.getContents (this)
If (sTransfenced null)
{
Try
{
String sPaste = (String) sTransf.getTransferData (DataFlavor.stringFlavor)
This.replaceRange (sPaste,getSelectionStart (), getSelectionEnd ())
}
Catch (UnsupportedFlavorException e)
{
}
Catch (IOException ioe)
{
}
}
}
Public void lostOwnership (Clipboard clipboard,Transferable contents)
{
}
Void doc_addedit (UndoableEditEvent e)
{
Undo.addEdit (e.getEdit ())
}
}
After reading this article, I believe you have a certain understanding of "how to use JAVA to achieve Undo, Redo,Copy, Paste, Cut". If you want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!