• We just launched and are currently in beta. Join us as we build and grow the community.

Undo Text using Ctrl Z in Java

luan45

Patch Analyst
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
139
Likes
123
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
This tutorial will teach you how to create a program that can undo a text using a shortcut key of ctrl+z in java.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of undoCtrlZ.java.

2. Import the following package library:
  1. import

    java.awt.event.*

    ;

    //used to access ActionEvent class
  2. import

    javax.swing.*

    ;

    //used to access AbstractAction,JFrame, JScrollPane, JTextArea, and KeyStroke class
  3. import

    javax.swing.event.*

    ;

    // used to access UndoableEditEvent and UndoableEditListener class
  4. import

    javax.swing.text.*

    ;

    // used to access the Document and JTextComponent class
  5. import

    javax.swing.undo.*

    ;

    // used to access CannotUndoException and UndoManager class

3. We will initialize variables in our Main, variable frame as JFrame, txtArea for JTextArea, undo as UndoManager and declare it as final because we will create an inner class on it, variable doc for the Document class.
  1. JFrame

    frame =

    new

    JFrame

    (

    )

    ;

    //instantiate frame
  2. JTextArea

    txtArea =

    new

    JTextArea

    (

    )

    ;

    //instantiate textArea
  3. final

    UndoManager

    undo =

    new

    UndoManager

    (

    )

    ;

    //instantiate an UndoManager
  4. Document

    doc =

    txtArea.getDocument

    (

    )

    ;

    //instantiate a Document class of the txtArea

4. Create an inner class for the UndoableEditEvent of the doc in our textArea. This will trigger that the textArea can be editable.
  1. doc.addUndoableEditListener

    (

    new

    UndoableEditListener

    (

    )

    {
  2. public

    void

    undoableEditHappened(

    UndoableEditEvent

    evt)

    {
  3. undo.addEdit

    (

    evt.getEdit

    (

    )

    )

    ;
  4. }
  5. }

    )

    ;

5. Create also an inner class that will have an undo event on the textArea. This will use the getActionMap method.
  1. txtArea.getActionMap

    (

    )

    .put

    (

    "Undo"

    , new

    AbstractAction

    (

    "Undo"

    )

    {
  2. public

    void

    actionPerformed(

    ActionEvent

    evt)

    {
  3. try

    {
  4. if

    (

    undo.canUndo

    (

    )

    )

    {
  5. undo.undo

    (

    )

    ;
  6. }
  7. }

    catch

    (

    CannotUndoException

    e)

    {
  8. }
  9. }
  10. }

    )

    ;

To have a ctrl z shortcut for the undo. Have this code below:
  1. txtArea.getInputMap

    (

    )

    .put

    (

    KeyStroke

    .getKeyStroke

    (

    "control Z"

    )

    , "Undo"

    )

    ;

6. Lastly, add the components, set the size and visibility to true, and close its operation.
  1. txtArea.getInputMap

    (

    )

    .put

    (

    KeyStroke

    .getKeyStroke

    (

    "control Z"

    )

    , "Undo"

    )

    ;

  2. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  3. frame.getContentPane

    (

    )

    .add

    (

    new

    JScrollPane

    (

    txtArea)

    )

    ;
  4. frame.setSize

    (

    380

    , 320

    )

    ;
  5. frame.setLocationRelativeTo

    (

    null

    )

    ;
  6. frame.setVisible

    (

    true

    )

    ;


Output:

ctrlz.png


Here's the full code of this tutorial:

  1. import

    java.awt.event.*

    ;

    //used to access ActionEvent class
  2. import

    javax.swing.*

    ;

    //used to access AbstractAction,JFrame, JScrollPane, JTextArea, and KeyStroke class
  3. import

    javax.swing.event.*

    ;

    // used to access UndoableEditEvent and UndoableEditListener class
  4. import

    javax.swing.text.*

    ;

    // used to access the Document and JTextComponent class
  5. import

    javax.swing.undo.*

    ;

    // used to access CannotUndoException and UndoManager class


  6. public

    class

    undoCtrlZ {
  7. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  8. JFrame

    frame =

    new

    JFrame

    (

    "Ctrl+Z Undo"

    )

    ;

    //instantiate frame
  9. JTextArea

    txtArea =

    new

    JTextArea

    (

    )

    ;

    //instantiate textArea
  10. final

    UndoManager

    undo =

    new

    UndoManager

    (

    )

    ;

    //instantiate an UndoManager
  11. Document

    doc =

    txtArea.getDocument

    (

    )

    ;

    //instantiate a Document class of the txtArea

  12. doc.addUndoableEditListener

    (

    new

    UndoableEditListener

    (

    )

    {
  13. public

    void

    undoableEditHappened(

    UndoableEditEvent

    evt)

    {
  14. undo.addEdit

    (

    evt.getEdit

    (

    )

    )

    ;
  15. }
  16. }

    )

    ;

  17. txtArea.getActionMap

    (

    )

    .put

    (

    "Undo"

    , new

    AbstractAction

    (

    "Undo"

    )

    {
  18. public

    void

    actionPerformed(

    ActionEvent

    evt)

    {
  19. try

    {
  20. if

    (

    undo.canUndo

    (

    )

    )

    {
  21. undo.undo

    (

    )

    ;
  22. }
  23. }

    catch

    (

    CannotUndoException

    e)

    {
  24. }
  25. }
  26. }

    )

    ;

  27. txtArea.getInputMap

    (

    )

    .put

    (

    KeyStroke

    .getKeyStroke

    (

    "control Z"

    )

    , "Undo"

    )

    ;

  28. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  29. frame.getContentPane

    (

    )

    .add

    (

    new

    JScrollPane

    (

    txtArea)

    )

    ;
  30. frame.setSize

    (

    380

    , 320

    )

    ;
  31. frame.setLocationRelativeTo

    (

    null

    )

    ;
  32. frame.setVisible

    (

    true

    )

    ;
  33. }
  34. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top