wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTextArea not focusing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextArea not focusing" Watch "JTextArea not focusing" New topic
Author

JTextArea not focusing

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
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

Joined: Jul 26, 2007
Posts: 10426
    
    8

Try requestFocusInWindow() and wrap it inside SwingUtilities.invokeLater()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
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

Joined: Feb 18, 2005
Posts: 362
Bingo!



Worked!

Why would it need to be wrapped within the SwingUtilities.invokeLater(new Runnable()?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Apr 01, 2009
Posts: 643
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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JTextArea not focusing