hi,
i want to write a console- or shell-like object (extends JTextPane).
if the user presses the up-key and down-key, he should be able to
"scroll" through his already entered commands.
i have it somehow realised with a KeyListener like that:
... implements KeyListener {
...
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
writeDecCommand();
setCaretPosition(getText().length());
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
writeIncCommand();
setCaretPosition(getText().length());
}
}
writeDecCommand and writeIncCommand are procedures which handle all
the reloading of the commands etc.
the problem i have: if the user presses VK_UP the caret jumps one
place up and then gets redirected to the end of the document. that
looks kind of ugly and i want to make my JTextPane to ignore the
keypress and just run my procedures!
i thought about making the caret invisible or something but that is of
no use, because when KeyReleased gets fired, the caret has already
moved.
i need something like a keyFilter like DocumentFilter or something.
i also want the user not to "scroll" the caret over all lines. just
allow it in the actual line horizontally.
any ideas out there?
thanks in advance,
Mark
hiwa - 06 Jan 2004 02:51 GMT
I haven't tried yet but I believe you must disable the default
KeyMap of the component in question. Get the KeyMap via getKeyMap()
and try calling its methods. And I hope you report the result on
the forum.
Jim Sculley - 06 Jan 2004 18:07 GMT
> I haven't tried yet but I believe you must disable the default
> KeyMap of the component in question. Get the KeyMap via getKeyMap()
> and try calling its methods. And I hope you report the result on
> the forum.
No, it is simply a matter of replacing the proper actions in the
ActionMap of the JTextPane and adding some actions in the InputMap.
Here is a quick and dirty example:
http://abraxis.com/niceguy/Test/Console.html
Jim S.

Signature
Remove my extraneous mandibular appendages to reply via e-mail