I am creating a Help System that uses links (a JButton extension) that expand and collapse subpanels with JLabels in them. The links and the collapsible panels work, but I'm having trouble implementing my find dialog. I want to be able to highlight parts of the text for which the user searches. I think my use of text attributes to underline the text in the links is messing with my ability to highlight the parts of the text, but I'm not sure how to do it differently. Here's the code for my Link class which my links subclass:
public abstract class Link extends JButton {
private static final int SPACE = 5;
private static final Color TEXT_COLOR = Color.BLUE;
public Link(String text) {
super(text);
setBorder(BorderFactory.createEmptyBorder(SPACE, SPACE, SPACE,
2 * SPACE));
setContentAreaFilled(false);
setFocusable(false);
setForeground(TEXT_COLOR);
Map<TextAttribute, Integer> underlineAttribute =
new HashMap<TextAttribute, Integer>();
underlineAttribute.put(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_ON);
setFont(getFont().deriveFont(underlineAttribute));
}
}
How can I implement highlighting text in my links without getting rid of the underlining? Do I need to change them to subclass something else?
One approach is to use HTML formatting for the button text. Of course, the path of least surprise for the end user would be if the buttons looked like buttons and the links looked like links (i.e. not buttons).
Should I subclass something else for the links?
For a link I'd generally use a JTextField
, as shown on my answer to How to change JButton?