aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to make a JTextArea gain focus only by mouseclick but not tabbing. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to make a JTextArea gain focus only by mouseclick but not tabbing." Watch "How to make a JTextArea gain focus only by mouseclick but not tabbing." New topic
Author

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

Dennis Zandvliet
Ranch Hand

Joined: Jun 19, 2008
Posts: 60
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

Joined: May 03, 2008
Posts: 4540
    
    5

Read the API for setFocusable(...)

edit And setEditable(...)

Note that a disabled component won't receive either keyboard or mouse events.


luck, db
There are no new questions, but there may be new answers.
Dennis Zandvliet
Ranch Hand

Joined: Jun 19, 2008
Posts: 60
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

Joined: May 03, 2008
Posts: 4540
    
    5

To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
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

Joined: Jun 19, 2008
Posts: 60
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

Joined: Aug 07, 2007
Posts: 2285
    
  49
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to make a JTextArea gain focus only by mouseclick but not tabbing.