JTextPane, user input character in different colors
Hi,
I have a class ColorPane extends JTextPane.
I want to control the user input on the pane in the way of having a "change tracking" functionality.
So when a user inserts text, it must be colored green. If the user deletes an existing character it must be colored red instead of really deleted.
But I'm struggling with this.
e.g. I've added a keyListener: addKeyListener(new KeyAdapter(){
publicvoid keyPressed(KeyEvent e){
switch (e.getKeyCode()){
case KeyEvent.VK_BACK_SPACE:
if (getCaretPosition() > 0){
changeColor(Color.RED);
String value = getText().substring(getCaretPosition()-1, getCaretPosition());
e.setKeyCode((int)value.toUpperCase().charAt(0));
break;
}
}
}
publicvoid keyReleased(KeyEvent e){
switch (e.getKeyCode()){
case KeyEvent.VK_BACK_SPACE:
setCaretPosition(getCaretPosition() - 1);
break;
}
}
}
But I can't get the deleted character to be colored red.
Also, if this coding is not the good approach, you can advice me if you want.
Best regards,
Tim
Message was edited by:
timvissers
# 1
Okay, I found out about extending DefaultStyledDocument.
But now I'm having the following problem: when removing by using the backspace button, my cursor should move to the left.
See code:
As you see, instead of removing, I put it in red. Side-effect is that the cursor is not moved when using backspace. Does someone know how to do this?
public void remove(int offset, int length) throws BadLocationException {
// super.remove(offset, length);//DO NOT REMOVE
doc.setCharacterAttributes(offset, length, red, true);// PUT IN RED!!
//if backspace set caret length to the left
//textPane.setCaretPosition(textPane.getCaretPosition()-length);
//else
//do nothing
# 2
Also, if this coding is not the good approach, you can advice me if you want.
I would use a [url http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#filter]Document Filter[/url].
You intercept the text before it is added to the Document and then you can set the Attributes to be whatever color you want.
You can also intercept the deletion of text. Instead of removing the character from the document you just reset the attributes.
This posting shows how you can do this:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=342068
# 3
Thanks.
Yes, I can change colors correctly now, but in the remove() of the document filter I don't know how to change the cursor after having done this (specifically how to detect whether the user pressed a VK_DELETE or a VK_BACK_SPACE.
Like in MS word change tracking: when VK_DELETE a character, the next character turns red and the cursor is moved one character to the right. When VK_BACK_SPACE a character, the previous character turns right and the cursor is moved one character to the left.
Both keys call the remove() method, but I don't know how I can make a distinction between those two, and how to move the cursor accordingly.
Thanks
# 4
[url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings[/url]
Create custom Actions for those two KeyStrokes. You use the getCaretPosition(...) method to get the character you want to change the attribute of and then use the setCaretPosition(...) method to change the position of the caret.
Note If I remember correctly the KeyStroke for the back space is non standard and you should use:
KeyStroke delete = KeyStroke.getKeyStroke("typed \010");