Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What listener to use in JTextArea when user double clicks to select text  RSS feed

 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JPanel with a small JTextArea that displays text bounded by newlines, ie, it looks to the use like a list of short, non-wrapping, Strings. When a line is double clicked the text in the line is selected and highlighted. What action listener can I use to detect this double click? In this context the line would be detected and copied to a JTextField for possible editting,

Thanks
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add a CaretListener to any JTextComponent to listen for CaretEvents, which get fired on selection changes. There's a nice little tutorial over at Oracle on how to write such a listener an examples on how to use it.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a line is double clicked the text in the line is selected and highlighted. What action listener can I use to detect this double click?


If you want to listen for a double click then you would use a MouseListener. It should be noted that a double click is used to select a word and a triple click is used to select a line.

Maybe a better design would be to use a JList. See List Editor for an approach that would allow you to edit a String in a JList when an item is double clicked or the Enter key is used. Remember that for proper GUI design the user should be able to use the keyboard or the mouse.
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both. Using a caret listener was not appropriate for what I wanted but I did learn from reading the tutorial.

I replaced the JTextArea with a JList and have it working just as I wanted.

This little app will be used by only one person for a short while but other parts of it will be honed and used in the main app I am developing. In this case I don't need keyboard input, but thanks for the reminder.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!