wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Trigger event when text is selected/deselected in  JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Trigger event when text is selected/deselected in  JTextField " Watch "Trigger event when text is selected/deselected in  JTextField " New topic
Author

Trigger event when text is selected/deselected in JTextField

Sachin H. Gupta
Greenhorn

Joined: Oct 10, 2012
Posts: 16
I want to add a Listener to JTextField so that buttons can be enabled/disabled if something is selected in the field.Which Listener should be used ??




[something.png]


[nothing.png]

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2172
    
  47
Welcome to the Ranch.

AFAIK there is no simple approach to solve this as there is no listener that responds to text selections.
You could however add a CaretListener and whenever the caret is moved check the JTextField to see if any of the text is selected (getSelectedText() returns null if nothing is selected) and if the highlighted state has changed set the button state accordingly.
Sachin H. Gupta
Greenhorn

Joined: Oct 10, 2012
Posts: 16
what is CaretListener ? I don't know what it does and when it is used. Can you please explain.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The first link returned when I searched the web for java caretlistener tutorial was this: How to Write a Caret Listener tutorial. I suggest you read that to find an answer to your question.
Sachin H. Gupta
Greenhorn

Joined: Oct 10, 2012
Posts: 16
Thanks guys, that helped.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trigger event when text is selected/deselected in JTextField
 
Similar Threads
JTable.clearSelection Doesn't Unselect JTextField
JTextField highlighted on windows
How to make all the text in a JField selected when JField is in focus?
Multiple listeners
JList enter key advice needed