import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.LinkedList; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.undo.UndoManager; public class UndoDemo extends JFrame { private UndoManager undo = new UndoManager(); private JTextArea textArea = new JTextArea(); public UndoDemo() { super("Undo Redo"); LinkedList cars = new LinkedList(); Container cp = getContentPane(); cp.add(textArea = new JTextArea(10, 30), BorderLayout.CENTER); JPanel bp; cp.add(bp = new JPanel(), BorderLayout.SOUTH); undo = new UndoManager(); textArea.getDocument().addUndoableEditListener(undo); JButton undoButton; bp.add(undoButton = new JButton("UNDO")); //cars.add(textArea); undoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (undo.canUndo()) { undo.undo(); } else { warn("Geri alinamaz"); } } }); JButton redoButton; bp.add(redoButton = new JButton("REDO")); redoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (undo.canRedo()) { undo.redo(); } else { warn("Ileri alinamaz"); } } }); pack(); setDefaultCloseOperation(EXIT_ON_CLOSE); } void warn(String msg) { JOptionPane.showMessageDialog(this, "ERROR: " + msg, "ERROR", JOptionPane.WARNING_MESSAGE); } public static void main(String[] files) { /* cars.add(); cars.add(); cars.add(); System.out.println(cars); */ new UndoDemo().setVisible(true); } }