This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • 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
when text is selected
[Thumbnail for something.png]
nothing.png
when nothing is selected
[Thumbnail for nothing.png]
 
Tony Docherty
Bartender
Posts: 2936
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
Pie
Posts: 20715
30
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