This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use "setCaretPosition"  RSS feed

 
ho mingchang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
public class TextAreaExample {
public static void main(String argv[]) {
Frame f = new Frame("TextArea Example");
f.setLayout(new FlowLayout());
String str = "1234567890 234567890 34567890 4567890 567890";
TextArea ta3 = new TextArea(str, 2, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);
f.add(ta3);
ta3.setCaretPosition(0);
f.pack();
f.setVisible(true);
}
}
When I compiled the file, it showed "Cannot set caret position until after the peer has been created". How should I modify the code?
Thank you very much for your help ^^
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, ho mingchang!
I'm moving this to the Swing / JFC / AWT forum...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message "Cannot set caret position until after the peer has been created" means that you can't call this method until you've added the TextArea to a container and made that container visible on the screen. Moving the call until after "setVisible(true)" should do the trick.
 
ho mingchang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works. Thank you very much for your help ^^
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!