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)
I think you have your answer right there in the link you provided.
Just write your own class extending the
JTextAreawhere each instance will be responsible for storing its highlights then use your own class instead.BTW @nIcE cOw great answer (+1 there).