• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextArea not focusing

 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys (again)

I have a JTextArea which I need to be 'focused' at the point of loading the panel it is on, but for some reason it only becomes focused if a switch away from my Swing app then back again (as if the screen is being repainted)
Here is the snippet of code if it helps with any ideas.

 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try requestFocusInWindow() and wrap it inside SwingUtilities.invokeLater()
 
Rob Camick
Ranch Hand
Posts: 2565
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the snippet of code if it helps


Not really because we don't know the context of how the code is used.

For example, you can't request focus on a component unless the frame is already visible.

To get around this problem Maneesh, suggested using a SwingUtilities.invokeLater(). But this will only work if your GUI code is actually being created on the EDT.

When you have a problem, post a SSCCE so we have all the information available and don't have to guess.
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo!



Worked!

Why would it need to be wrapped within the SwingUtilities.invokeLater(new Runnable()?
 
Paul Clapham
Sheriff
Posts: 20982
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith SmithA wrote:Why would it need to be wrapped within the SwingUtilities.invokeLater(new Runnable()?

Rob Camick wrote:For example, you can't request focus on a component unless the frame is already visible.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, with a few exceptions, any GUI work needs to be done in the event thread. Even the creating of the GUI (this didn't used to be true, but is now recommended due to some components executing non-thread-safe code before the GUI is made visible).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic