• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need to give JTextArea the focus; using it on a panel and with a JToolBar

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a program with a main panel JPanel that implements KeyListener. I added a JTextArea to which I added as the KeyListener: this, meaning the main panel. I made the JTextArea fill the entire main panel because I want to catch a key press anywhere in the main panel.
This is working just fine: I catch the key presses and the program does what I want it to.

I added a JToolBar to the program; it is working just fine.

So what's the problem?

Now that I have two components on the main panel - toolbar and panel with textarea -, the textarea must have the focus before it will send the KeyEvent and call keyPressed(). I don't want to expect the user to click on the main window to get the program to start or after each time they use the toolbar.

I have tried calling both requestFocus() and requestFocusInWindow() on the textarea; neither call helped.
I put the toolbar at PAGE_END instead of PAGE_START; this worked to allow the program to start but I don't want to have the toolbar at the bottom and once I clicked on a toolbar button, the textarea lost the focus.

I need to be able to give the textarea the focus when the program starts and after the user uses a toolbar button. (I suspect that if I solve the problem when the program starts, I can use the same method after handling a button press.)

Anyone know how I can do this?

Thanks.
 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rosie,

If you want your KeyListener to work only on JTextArea, try adding KeyListener to JTextArea.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to catch a key press anywhere in the main panel.



You should be using Key Bindings to listen for a specific KeyStroke, not a KeyListener. You can add the Key Bindings to the Root Pane of the frame to listen for KeyStrokes any where in the frame.

I have tried calling both requestFocus() and requestFocusInWindow() on the textarea; neither call helped.



The proper method so use is requestFocusInWindow(), but this method only works once the frame is visible.

and after the user uses a toolbar button.



Make your toolbar buttons non-focusable by using setFocusable( false );
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
Thank you.
Your suggestion to set the toolbar buttons setFocusable(false) worked. Now the textarea has the focus when the program starts and after the user presses a toolbar button.

I will look into Key Bindings; I'm somewhat familiar with them but haven't used them much. They may solve another problem that I'm having that I was going to start another topic about.

Rosie

Update:
Key Bindings worked perfectly and did solve the other problem I had which was caused by the need to have the JTextArea fill the panel. I was able to remove the JTextArea and Key Bindings are a lot cleaner than what I was doing.

Thank you
 
There's a city wid manhunt for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic