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
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