|
-
November 20th, 2003, 05:54 PM
#1
help with manipulating text in JPanel
hi all
i am writing an editor like MS Paint, my canvas is extending JPanel. I provide the facility to draw basic shape and text. I am getting trouble with text.
I have TextResource class which maintains input character in String and manipulate the key with StringBuffer when an character is typed and use TextLayout to display for user.
So I have to handle all input key stroke and i can handle BackSpace, Left and Right arrow key. But I cannot handle Space and Tab and the rest of whitespace.
So I am having the second thought that the java classes I used might not be appropriate. So please help me. Any suggestion will be appreciated! Thank you very much for your time.
here is some important code:
// in order to add a character into TextResource
// please ignore EBObjectAttribute, I used it for Property Table.
Code:
public void addCharacter(char c, int code) {
try {
StringBuffer buffer = new StringBuffer(text.getStringValue());
if (code == KeyEvent.VK_BACK_SPACE) {
if (this.cursorPosition > 0)
buffer.deleteCharAt(this.cursorPosition-1);
handleArrowKey(false);
this.text = new EBObjectAttribute(this, buffer.toString());
}
else if (code == KeyEvent.VK_LEFT || code == KeyEvent.VK_RIGHT) {
handleArrowKey(code == KeyEvent.VK_RIGHT);
}
else if (code == KeyEvent.VK_DELETE) {
if (this.cursorPosition >= 0 && this.cursorPosition < text.getStringValue().length())
buffer.deleteCharAt(this.cursorPosition);
this.text = new EBObjectAttribute(this, buffer.toString());
}
else {
if (Character.isDefined(c) && (code == KeyEvent.VK_SPACE || !Character.isWhitespace(c)))
buffer.insert(this.cursorPosition, c);
this.text = new EBObjectAttribute(this, buffer.toString());
handleArrowKey(true);
}
}
catch (Exception e) { }
}
// in order to handle cursor position: code from Java sun Tutorial
Code:
protected void handleArrowKey(boolean rightArrow) {
try {
if (text == null || text.getStringValue().length() == 0) {
this.cursorPosition = 0;
return;
}
textLayout = new TextLayout(this.text.getStringValue(), font, new FontRenderContext(null, false, false));
}
catch (Exception e) { }
TextHitInfo newPosition;
if (rightArrow)
newPosition = textLayout.getNextRightHit(this.cursorPosition);
else
newPosition = textLayout.getNextLeftHit(this.cursorPosition);
if (newPosition != null)
this.cursorPosition = newPosition.getInsertionIndex();
}
Last edited by nnmaw; November 20th, 2003 at 06:00 PM.
-
November 20th, 2003, 09:01 PM
#2
Try using JTextArea or JEditorPane instead of using TextLayout
-
November 21st, 2003, 09:05 AM
#3
hi
thanks for your reply.
i thought about to use those, but i don't know how to use them in my application. because my application is not just showing them permanently by giving x,y coordinate. like in MS Paint, in order to add Text, you click on Text button on and click on canvas to let application know where your text is supposed to be. and application gives you a box where you can input text. so i am not sure how to use them. and is it going to fix my problem with getting input characters?
any suggestion is appreciated. thanks a lot.
-- naing.
Last edited by nnmaw; November 21st, 2003 at 09:57 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|