4

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?

ouflak
  • 2,458
  • 10
  • 44
  • 49
Eva
  • 4,397
  • 5
  • 43
  • 65

1 Answers1

4

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?

E.G.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • What does the "buttons looked like buttons and the links looked like links" mean? – Eva Nov 08 '11 at 02:08
  • Do links and buttons look the same to you? Which part do you not understand? – Andrew Thompson Nov 08 '11 at 02:29
  • My links are subclasses of JButton that look like links. Should I subclass something else for the links? – Eva Nov 08 '11 at 02:33
  • Perhaps I under estimated how much your buttons look like links (if it is not an SSCCE, I don't examine code closely). In any case, see the edit to my answer. – Andrew Thompson Nov 08 '11 at 02:42