Swing - Detect how focus was moved?

M

MikL

Hi all,

I have a Swing UI containing JTextFields. When the user moves the focus
into a JTextField using the keyboard (eg. by pressing TAB), I want to use
selectAll() so that the user can then overwrite the field contents without
messing with the mouse or having to type multiple backspaces or other
keyboard gymnastics. However, I _don't_ want to selectAll() if focus was
moved to the field using the mouse.

Currently I'm looking at monitoring the sequence of FocusListener and
MouseListener events to determine whether the user used the keyboard or
mouse to move focus, but it seems unelegant and relies on eg. mousePressed()
running after requestFocus() and before focusGained(). Is there a better
way to detect how the focus was moved?

Regards,
MikL
 
A

Andrei Kouznetsov

Currently I'm looking at monitoring the sequence of FocusListener and
MouseListener events to determine whether the user used the keyboard or
mouse to move focus, but it seems unelegant and relies on eg. mousePressed()
running after requestFocus() and before focusGained(). Is there a better
way to detect how the focus was moved?

may be you could check caret position, this works however only if user
clicks between first an last character.
 
A

Ann

MikL said:
Hi all,

I have a Swing UI containing JTextFields. When the user moves the focus
into a JTextField using the keyboard (eg. by pressing TAB), I want to use
selectAll() so that the user can then overwrite the field contents without
messing with the mouse or having to type multiple backspaces or other
keyboard gymnastics. However, I _don't_ want to selectAll() if focus was
moved to the field using the mouse.

Currently I'm looking at monitoring the sequence of FocusListener and
MouseListener events to determine whether the user used the keyboard or
mouse to move focus, but it seems unelegant and relies on eg. mousePressed()
running after requestFocus() and before focusGained(). Is there a better
way to detect how the focus was moved?

Regards,
MikL
Not sure, but can you set a flag in "mouse listener?"
 
M

MikL

Not sure, but can you set a flag in "mouse listener?"

Yes, I can set a flag if mousePressed() was preceded by a requestFocus()
that hasn't yet been followed by a focusGained() (and the control doesn't
yet have focus), but that involves making what I believe is an undocumented
assumption about the order of mouse vs. focus events that might change in a
different release of Swing. It also doesn't cater for the situations in
which the requestFocus() is never followed by a focusGained(), thus leaving
"focus pending" flags set indefinitely. It feels like a fragile design that
I'd rather avoid if I can.
 
A

Andrei Kouznetsov

Yes, I can set a flag if mousePressed() was preceded by a requestFocus()
that hasn't yet been followed by a focusGained() (and the control doesn't
yet have focus), but that involves making what I believe is an undocumented
assumption about the order of mouse vs. focus events that might change in a
different release of Swing. It also doesn't cater for the situations in
which the requestFocus() is never followed by a focusGained(), thus leaving
"focus pending" flags set indefinitely. It feels like a fragile design that
I'd rather avoid if I can.

don't use boolean flag.
every event has a timestamp, you can compare timestamps of your
events and if differense is _very_small_ then you know it was focus change
after mouse click.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top