peer component for setCaretPostion
Does anybody know how to set a peer before calling setCaretPosition? I am getting this error when calling setCaretPosition to scroll my text to the end of the text area:
"Cannot set caret position until after the peer has been created"
I am relatively new to Java and don't know how to create a peer in this case. All of the books I have read don't include this information. I appreciate any help anybody can provide.
Thanks.
[459 byte] By [
hlongster] at [2007-9-26 2:11:01]

Here is my sample code. I think I have to set up the text handler somewhere, but it doesn't work, so I just called setCaretPosition directly, when the error occurs.
import java.awt.*;
import java.awt.event.*;
class Main extends Frame {
TextArea textArea = new TextArea("This is a test\nFor \
setCaretPosition()\nThis is a test\nFor setCaretPosition()\n \
This is a test\nFor setCaretPosition()\nThis is a test\nFor \
setCaretPosition()\nThis is a test\nFor \
setCaretPosition()\nThis is a test\nFor \
setCaretPosition()\nThis is a test\nFor setCaretPosition()\n");
Main() {
super("setCaretPosition Example");
textArea.setSize(400, 400);
add(textArea);
textArea.isEditable();
textArea.setCaretPosition(textArea.getText().length());
//textArea.addKeyListener(new KeyEventHandler());
pack();
show();
}
class KeyEventHandler extends KeyAdapter {
public void keyReleased(KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("Entering area");
textArea.setCaretPosition(20000);
}
}
}
static public void main(String[] args) {
new Main();
}
}
Yup, here's your error:
super("setCaretPosition Example");
textArea.setSize(400, 400);
super.add(textArea);
super.setVisible();//you need this!!
You should be all set after that...I tested it and it worked fine. Let me know if this helped.
Shrubz