0

I have a project where some random text is displayed in a JScrollPane. This text is coded in and is not editable. Is there a way to create a mouse click event on each line of text and have that event take the text clicked on and have it display in a textField?

The part that stumps me is how to act on a line of text versus a clickevent on a button per se. Below is a rendering of the project and the areas containing the text.

enter image description here

Kevin Schultz
  • 886
  • 3
  • 20
  • 42
  • Looks to me like it should be done as a JList. I recommend looking here: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html – ThePerson Jun 27 '14 at 20:35
  • Might be this [answer](http://stackoverflow.com/a/10463120/1057230) be of some help on the topic. – nIcE cOw Jun 28 '14 at 02:57

1 Answers1

1

Presumably your text is displayed in a JTextArea or JTextPane, so you would add a MouseListener to the component. Then when the MouseEvent is generated you can get the caret position. Using the caret position you can then use the Utilities class. It has methods like:

  1. getRowStart(...)
  2. getRowEnd(...)

Using these values you can then get the text from the Document using the getText(...) method.

camickr
  • 321,443
  • 19
  • 166
  • 288