Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rosie,

If you want your KeyListener to work only on JTextArea, try adding KeyListener to JTextArea.
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!