I have a JLabel inside a JPanel. I’m using Netbeans IDE for the GUI of my java program (free design, and no Layout Manager).
At runtime I need to highlight a part of my JLabel. Highlighting a part of JLabel can be done as said here : How to highlight part of a JLabel?
I don’t know how to do that in run time, I mean I already have a JLabel, now how can I override it’s paintComponent() method for that purpose ?
Here is an illustrative example of the answer given by Tikhon Jelvis in the post you mentioned, all you need is to add some fields (in this example
startandend) to indicate the regions to be highlighted, and use a method (highlightRegionin this example) to set these fields:Notice the use of
label.setVerticalAlignment(JLabel.TOP);to simplify things. If you don’t use it then you need to do extra computations to determine the exact vertical location of the region to be highlighted.