• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Set Caret Cursor inJTextArea

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)'
 
reply
    Bookmark Topic Watch Topic
  • New Topic