can i have more then one JTextArea and JTextField for one inner class (MyUndoableEditListener())?

.
.

 MainTabbed mPane = new MainTabbed();
.
.
.
 mPane.area.getDocument().addUndoableEditListener(new MyUndoableEditListener()); 
         mPane.locField.getDocument().addUndoableEditListener(new MyUndoableEditListener()); 
         mPane.dateField.getDocument().addUndoableEditListener(new MyUndoableEditListener()); 
         mPane.ndBuddyField.getDocument().addUndoableEditListener(new MyUndoableEditListener()); 
         mPane.depthField.getDocument().addUndoableEditListener(new MyUndoableEditListener()); 
         mPane.firstBuddy.getDocument().addUndoableEditListener(new MyUndoableEditListener()); 
         mPane.weightField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.timeInField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.tOutField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.startTimeField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.sTimeField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.bTimeField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.startField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.finishField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.airField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.surfaceField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.bottomField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.equipmentArea.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.waterTypeField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.visiField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.diveLogField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
         mPane.diveNoField.getDocument().addUndoableEditListener(new MyUndoableEditListener());
      
.
.
.

 class UndoAction extends AbstractAction{ 
          public UndoAction(){ 
            super("Undo"); 
            setEnabled(false); 
         } 
          public void updateUndoState(){ 
            if (undo.canUndo()){ 
               setEnabled(true); 
               putValue(Action.NAME, undo.getUndoPresentationName()); 
            } 
            else { 
               setEnabled(false); 
               putValue(Action.NAME, "Undo"); 
            } 
         } 
          public void actionPerformed(ActionEvent e){ 
            try{ 
               undo.undo(); 
            } 
                catch (CannotUndoException ex){} 
            updateUndoState(); 
            redoAction.updateRedoState(); 
         } 
      }
   
       class RedoAction extends AbstractAction{ 
          public RedoAction() { 
            super("Redo"); 
            setEnabled(false); 
         } 
          public void actionPerformed(ActionEvent e){ 
            try{ 
               undo.redo(); 
            }
                catch(CannotRedoException ex){} 
            updateRedoState(); 
            undoAction.updateUndoState(); 
         } 
          protected void updateRedoState() { 
            if (undo.canRedo()) { 
               setEnabled(true); 
               putValue(Action.NAME, undo.getRedoPresentationName()); 
            } 
            else { 
               setEnabled(false); 
               putValue(Action.NAME, "Redo"); 
            } 
         }
      }
   
       class MyUndoableEditListener implements UndoableEditListener { 
          public void undoableEditHappened(UndoableEditEvent e) { 
         //Remember the edit and update the menus. 
            undo.addEdit(e.getEdit()); 
            undoAction.updateUndoState(); 
            redoAction.updateRedoState(); 
         } 
      }

it seems doesn't work @@a, it only works for the first JTextArea but the second JTextArea cant work.
or should i go and create a new inner class (MyUndoableEditListener()) for each JTextArea ?
and is this undo manager working for JTextField ?

They can share the same UndoableEditListener. Example

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;


public class UndoDmeo extends javax.swing.JFrame {
    private javax.swing.JTextField txt1;
    private javax.swing.JTextField txt2;

    final UndoManager undo = new UndoManager();

    final UndoableEditListener editListener = new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }
    }; 
    
    public UndoDmeo() {
        initComponents();
        getRootPane().getActionMap().put("Undo",
          new AbstractAction("Undo") {
              public void actionPerformed(ActionEvent evt) {
                  try {
                      if(undo.canUndo()) {
                          undo.undo();
                      }
                  } catch(CannotUndoException e) {
                  }
              }
          });
        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
           KeyStroke.getKeyStroke("control Z"), "Undo");
        
        txt1.getDocument().addUndoableEditListener(editListener);
        txt2.getDocument().addUndoableEditListener(editListener);
    }

    private void initComponents() {

        txt1 = new javax.swing.JTextField();
        txt2 = new javax.swing.JTextField();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.FlowLayout());

        txt1.setPreferredSize(new java.awt.Dimension(100, 20));
        getContentPane().add(txt1);

        txt2.setPreferredSize(new java.awt.Dimension(100, 20));
        getContentPane().add(txt2);

        pack();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UndoDmeo().setVisible(true);
            }
        });
    }
}

understood, thanks a millions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.