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
jay - 25 Feb 2006 00:57 GMT
Well I guess I found a weakness in this group...
Luc The Perverse - 25 Feb 2006 01:24 GMT
> Well I guess I found a weakness in this group...
LOL - it's only been 19 hours since you asked your question.
--
LTP
:)
jay - 25 Feb 2006 02:18 GMT
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
IchBin - 25 Feb 2006 04:14 GMT
> Nevermind, I answered my own question -- in case anybody cares here is
> what you have to do to make it work:
[quoted text clipped - 28 lines]
>
> .jay
http://www.javaworld.com/javaworld/jw-06-1998/jw-06-undoredo.html

Signature
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-)
Roedy Green - 25 Feb 2006 09:48 GMT
>Well I guess I found a weakness in this group...
see http://mindprod.com/jgloss/newsgroups.html
for why you are not getting the instant help you so entitled to.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
jay - 25 Feb 2006 18:39 GMT
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."