• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trigger event when text is selected/deselected in JTextField

 
Sachin H. Gupta
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
[Thumbnail for something.png]
when text is selected
nothing.png
[Thumbnail for nothing.png]
when nothing is selected
 
Tony Docherty
Bartender
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is CaretListener ? I don't know what it does and when it is used. Can you please explain.
 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, that helped.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic