Back Space keyStroke with JTextPane
Dear all,
I have two texpane, and i want to overwite back space behaviour:
That is to say :
if( XCondition = true){
do nothing (NO BACKSPACE OCCURS)
}else{
do DEFAULT BACK SPACE behaviour for JTextPane
}
I have tried to do :
//get default back space action listener
ActionListener backspaceActionListener = textPane.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE,0));
//create a new class with embedded action listener
textPane.getKeymap().addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE,0), new BackwardAction(backspaceActionListener));
source code example:
privateclass BackwardActionextends AbstractAction{
private ActionListener defaultActionListener;
public BackwardAction(ActionListener actionListener){
defaultActionListener = actionListener;
}
publicvoid actionPerformed(ActionEvent e){
//perform default arrow left textpane action if we go forward prompt.
//here we fired and propagate the catched left key event to the default action listener.
if (getCaretPositionOnLine() > commandPrompt.length()){
//fire a default action
defaultActionListener.actionPerformed(e);
}else{
//Here we do nothing if we go backward prompt.
}
}
}
This is seems to performed correctly, but if i have a second textpane with the same
constraint then backspace is running well on the first jtextpane but no on the second.
Why?
Thanks for all assistance
[2279 byte] By [
superman@a] at [2007-10-2 16:15:33]

I solve my problem with the rebind() methods in BackwardAction class that i have found with the specific link.
private class BackwardAction extends AbstractAction{
private ActionListener actionListener;
public BackwardAction(ActionListener actionListener) {
this.actionListener = actionListener;
rebind();
}
public void actionPerformed(ActionEvent e) {
//perform default arrow left textpane action if we go forward prompt.
//here we fired and propagate the catched left key event to the default action listener.
if (getCaretPositionOnLine() > commandPrompt.length()) {
System.out.println("> commandPrompt.length()");
actionListener.actionPerformed(e);
} else {
//Here we do nothing if we go backward prompt.
}
}
private void rebind(){
InputMap im = textPane.getInputMap();
// remove old binding
KeyStroke typed010 = KeyStroke.getKeyStroke("typed \010");
im.remove(typed010);
//rebind backspace
KeyStroke bksp = KeyStroke.getKeyStroke("BACK_SPACE");
KeyStroke ctrlH = KeyStroke.getKeyStroke("ctrl H");
//add new binding
im.put(bksp, DefaultEditorKit.deletePrevCharAction);
im.put(ctrlH, "none");
}
Nevertheless, i'm confused with keyMap() and InputMap().Indeed if i perform the following code
//Do i get the textpane default behaviour with getActionForKeyStroke?
ActionListener backspaceActionListener = textPane.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE,0));
//Use Input map and Action MaptextPane.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE,0),"backward");
textPane.getActionMap().put("backward",new BackwardAction(backspaceActionListener));
Backward action occurs whatever i do and i would want that nothing happen if we go backward prompt.Why?
Now if i execute the following code all running well?
//Do i get the textpane default behaviour with getActionForKeyStroke?
ActionListener backspaceActionListener = textPane.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE,0));
//UseKeyMap
textPane.getKeymap().addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE,0), new BackwardAction(backspaceActionListener));
Thanks in advance for all your assistance.