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]
# 1
Hey, your error is occurring cause your Text Area is in an invalid state...post your code and we'll see what's happening.Shrubz
shrubz at 2007-6-29 9:02:36 > top of Java-index,Archived Forums,Java Programming...
# 2

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();

}

}

hlongster at 2007-6-29 9:02:36 > top of Java-index,Archived Forums,Java Programming...
# 3

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

shrubz at 2007-6-29 9:02:36 > top of Java-index,Archived Forums,Java Programming...
# 4
sorry...super.setVisible(true);...woops =)Shrubz
shrubz at 2007-6-29 9:02:36 > top of Java-index,Archived Forums,Java Programming...
# 5
It works. Thanks very much...
hlongster at 2007-6-29 9:02:36 > top of Java-index,Archived Forums,Java Programming...