Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make a JTextArea gain focus only by mouseclick but not tabbing.

 
Dennis Zandvliet
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The behaviour I want is, that a specific disabled JTextArea is not part of the focus cycle, but when I click on it with a mouse the JTextArea:

-becomes enabled
-gets focus
-and the text inside becomes selectable for copying

Then As soon as I tab inside the JTextArea or click on another component, the the JTextArea loses focus and is *not* part of the focus cycle anymore.

In other words, even though the JTextArea is not part of focus cycle root, i want to be able to click on for the sake of selecting and copying the text inside this component

For an example of this behaviour see the preferences panel of the Eclipse editor. (Version 3.7.0)

menu-> window-> preferences
and inside the preferences window select:

in the tree -> general -> Appearance -> Colors and Fonts.

And check the behaviour of the 'Description' JTextArea field.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the API for setFocusable(...)

edit And setEditable(...)

Note that a disabled component won't receive either keyboard or mouse events.
 
Dennis Zandvliet
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already read 'How to Use the Focus Subsystem' and those API's. I've also tried a Custom FocusTraversalPolicy, but it didn't work.

I'm thinking of combining a Custom FocusTraversalPolicy with a MouseListener. But since their are already other listeners defined for these components I'm afraid that you might get unexpected/undesired behaviour.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
 
Tony Docherty
Bartender
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a simple test class that disables the the JTextArea which effectively removes it from the focus cycle but stills allows you to click in it to select it. Once you move out of the JTextArea it is disabled again.
Note: to tab out of the JTextArea you will need to use CTRL+TAB as TAB is handled by JTextArea to insert a tab into the text area.

This does what you requested but I'm not convinced being able to click into a disabled field to edit it is particularly intuitive.
 
Dennis Zandvliet
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your example code, I will give it a try.

Tony Docherty wrote:Here's a simple test class that disables the the JTextArea which effectively removes it from the focus cycle but stills allows you to click in it to select it. Once you move out of the JTextArea it is disabled again.
Note: to tab out of the JTextArea you will need to use CTRL+TAB as TAB is handled by JTextArea to insert a tab into the text area.


I guess this can be fixed by changing focus traversal keys from CTRL+TAB to Tab?

Tony Docherty wrote:
This does what you requested but I'm not convinced being able to click into a disabled field to edit it is particularly intuitive.


In hindsight I didn't specify my problem correctly.
All i want is that some readonly (not editable?) fields textfields are textareafields are not part of the normal tab focus cycle, but that users are still able to click on such readonly fields to select and copy text. So probably in this context the terms/properties ' enabled or disabled' are not correct.
 
Tony Docherty
Bartender
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess this can be fixed by changing focus traversal keys from CTRL+TAB to Tab?

CTRL+TAB is a standard traversal key so I wouldn't remove it but you can try adding TAB. Not sure if this will solve all your problems though, you may have to do something to stop the JTextArea inserting a tab into the text as well. But as I say I'm not sure of this, some experimentation is required.

In hindsight I didn't specify my problem correctly...

In that case change the setEnabled() method calls to setFocusable() and that should provide the same features without disabling the JTextArea.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic