2

I have a JTextArea where lines are highlighted upon right-click. While I could achieve this (using some great help from my previous post), however I would also want some kind of a toggle functionality i.e. if a line is not highlighted, right-click would highlight it and vice-versa.

With reference to this, is it possible to check whether the selected line is already highlighted or not? Comments/Suggestions?

I understand one way of doing this is to keep a track of all the highlights (maybe using a HashMap as shown in the example here) but this would slightly add to the complexity because I'm writing this method in a helper class that serves to multiple tabs containing these text areas. Any other alternative solution would be very helpful (as in toggling the highlights)

Community
  • 1
  • 1
Sujay
  • 6,753
  • 2
  • 30
  • 49

2 Answers2

3

I think you have your answer right there in the link you provided.

Just write your own class extending the JTextArea where each instance will be responsible for storing its highlights then use your own class instead.

BTW @nIcE cOw great answer (+1 there).

Community
  • 1
  • 1
Boro
  • 7,913
  • 4
  • 43
  • 85
2

SWT has a StyledText class.

You could write a styled text class for Swing, implementing the Document interface, that keeps track of the highlights and whatever other text decorations you want to keep track of. This class would encapsulate the methods that determine the decorations at a given character position in the styled text.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111