Highlighting Links

空扰寡人 提交于 2019-12-04 04:29:43

问题


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? Thanks in advanced!


回答1:


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.



来源:https://stackoverflow.com/questions/8044694/highlighting-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!