• Post Reply Bookmark Topic Watch Topic
  • New Topic

Set Caret Cursor inJTextArea  RSS feed

 
Vincent Tomas
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I am very frustrated. I am trying to set my cursor at the beginning of the second line of my JTextArea but the cursor doesn't show when I uncomment the block of code in my JNotePad constructor, why?

myFile.txt has just one sentence that says, "This is a test of your local broadcast".

Thank you for your assistance.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> but the cursor doesn't show when I uncomment the block of code in my
> JNotePad constructor, why?

because the focus is on the 'save' button.

uncomment the block
run the program
tab 3 times (so textArea now has focus)
cursor should be blinking

fix is to add this line to main
frame.setVisible(true);
ta.requestFocusInWindow();//<---must be AFTER setVisible

because you're creating the frame in main(), you'll need to make 'ta' static
 
Vincent Tomas
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Michael! I was about to go blind. However, without running the application. What makes the Save button have focus? As you can tell I am newbie with GUI stuff.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> What makes the Save button have focus?

comment out the requestFocusInWindow()
run the program again, just to make sure it's the same as before (no blinking cursor)
make one slight change:
//add(north, BorderLayout.NORTH);
add(north, BorderLayout.SOUTH);//add buttons to bottom, instead
run program again

do you understand what's happening now?
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
According to the Java Tutorial:

If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed.

Good Luck,
Avi.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, but most people don't know when a frame is 'realized'

far simpler to tell them 'after setVisible(true)'
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!