Undo Operation Struggle

J

jay

Hi all,

I have a JTabbedPane that contains a JTextArea as the component for
each tab. Before I add the text area to the JTabbedPane I bind it to
the ctr-z and ctr-y key strokes for undo and redo. I Also have 2
JButtons which I add the action for the undo and redo commands and then
I add these in my main toolbar.

My problem: The shortcuts ctr-z and ctr-y work fine but the JButtons in
my main toolbar do not work when more than one tab is opened in the
JTabbedPane. The undo and redo buttons otherwise work fine when I have
only one tab but fail when more than one tab is open, even though the
ctr-z and ctr-y work fine.

Here is how I am adding the undo/redo operations to each JTextArea:

----------------------------
public class UndoRedo {

JButton undo_command = new JButton("Undo");
JButton redo_command = new JButton("Redo");

/**
* Adds Undo/Redo capabilities to the passed in JTextArea, it also
binds
* the JTextArea with the Ctrl-z and Ctrl-y key strockes.
*/
public void addUndoRedo(JTextArea area) {

final UndoManager undo = new UndoManager();
Document doc = area.getDocument();

// Listen for undo and redo events
doc.addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent evt) {
undo.addEdit(evt.getEdit());
}
});

//action for the undo command
AbstractAction undo_action = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canUndo()) {
undo.undo();
}
} catch (CannotUndoException e) {
e.printStackTrace();
}
}
};
//action for the redo command
AbstractAction redo_action = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canRedo()) {
undo.redo();
}
} catch (CannotRedoException e) {
e.printStackTrace();
}
}
};

// Create an undo action and add it to the text component
area.getActionMap().put("Undo",undo_action);
undo_command.setAction(undo_action);

// Bind the undo action to ctl-Z
area.getInputMap().put(KeyStroke.getKeyStroke("control Z"),
"Undo");

// Create a redo action and add it to the text component
area.getActionMap().put("Redo", redo_action);
redo_command.setAction(redo_action);

// Bind the redo action to ctl-Y
area.getInputMap().put(KeyStroke.getKeyStroke("control Y"),
"Redo");

//add icons to the Undo/Redo buttons
//imageUndoRedo();

}
}

-----------------------------

Does anyone know of why this is happening, I have tried everything but
nothing seems to work. Any help is greatly appreciated.

..jay
 
J

jay

Nevermind, I answered my own question -- in case anybody cares here is
what you have to do to make it work:
--------------------------------------
undo_command.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
if (undo.canUndo()) {
undo.undo();
}
} catch (CannotUndoException e) {
e.printStackTrace();
}
}
});
redo_command.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
if (undo.canRedo()) {
undo.redo();
}
} catch (CannotRedoException e) {
e.printStackTrace();
}
}
});

----------------------------------

It's a dirty way of doind it, but hey, it works. If anyone finds out a
better way please let me know.

..jay
 
I

IchBin

jay said:
Nevermind, I answered my own question -- in case anybody cares here is
what you have to do to make it work:
--------------------------------------
undo_command.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
if (undo.canUndo()) {
undo.undo();
}
} catch (CannotUndoException e) {
e.printStackTrace();
}
}
});
redo_command.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
if (undo.canRedo()) {
undo.redo();
}
} catch (CannotRedoException e) {
e.printStackTrace();
}
}
});

----------------------------------

It's a dirty way of doind it, but hey, it works. If anyone finds out a
better way please let me know.

.jay
http://www.javaworld.com/javaworld/jw-06-1998/jw-06-undoredo.html

--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
J

jay

A lady was picking through the frozen turkeys at the grocery store,
but she couldn't find one big enough for her family. She asked a stock
boy,

"Do these turkeys get any bigger?"

The stock boy replied, "No ma'am, they're dead."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top