将工具提示添加到JTextPane [英] Adding tooltips to JTextPane

查看:114
本文介绍了将工具提示添加到JTextPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JTextPane中只添加一些工具提示。例如,如果JTextPane中有引用链接文本,我想在该文本中添加工具提示以显示链接。有什么方法可以实现这个功能吗?

I want to add some tooltips to only a certain text inside a JTextPane. As an example, if there is a reference link text inside the JTextPane I want to add a tooltip to that text to show the link. Is there any way I can achieve this functionality?

推荐答案

好问题。

First Swing支持HTML,因此要显示带有链接的工具提示,您只需说:

First Swing supports HTML, so to show tooltip with link you just have to say:

comp.setToolTipText(< html> < a href ='http://www.google.com'> google< / a>< / html>);

问题在于此工具提示是否可点击。

The problem is making this tooltip clickable.

不幸的是,它不是由Swing本身完成的。

Unfortunately it is not done by Swing itself.

工具提示由ToolTipManager创建。当你调用setToolTipText()时,Jcomponent将自己的实例添加到Tooltip管理器的共享实例,该实例负责显示工具提示(使用无法覆盖的方法 show()。也不能改变工具提示管理器本身。

Tooltip is created by ToolTipManager. When you call setToolTipText() Jcomponent adds the instance of itself to shared instance of Tooltip manager that is responsible on showing the tooltip (using method show() that cannot be overridden. You cannot change the tooltip manager itself too.

因此,我建议的最佳解决方案是执行以下操作。
您可以使用<听取AWT事件code> Toolkit.getDefaultToolkit()。addAWTEventListener()

So, the best solution I can suggest is to do the following. You can listen to the AWT events using Toolkit.getDefaultToolkit().addAWTEventListener()

因此,当显示工具提示时,抓住它,发现并添加鼠标监听器。这个鼠标监听器将使工具提示本身可以点击。

So, when tooltip is being showed catch it, discover, and add mouse listener on it. This mouse listener will make the tooltip itself clickable.

这是我刚刚写的练习。你可以用它作为参考。祝你好运。

Here is the exercise I have just written. You can use it as a reference. Good luck.

    public static void main(String[] args) throws InterruptedException, InvocationTargetException {
        final JFrame f = new JFrame("test");
        f.setSize(100, 100);


        JLabel l = new JLabel("<html><a href='http://www.google.com'>google</a></html>");
        l.setToolTipText("<html><a href='http://www.google.com'>google</a></html>");


        long mask = AWTEvent.COMPONENT_EVENT_MASK |
//      AWTEvent.CONTAINER_EVENT_MASK |
//      AWTEvent.FOCUS_EVENT_MASK |
//      AWTEvent.KEY_EVENT_MASK |
//      AWTEvent.MOUSE_EVENT_MASK |
//      AWTEvent.MOUSE_MOTION_EVENT_MASK |
        AWTEvent.WINDOW_EVENT_MASK |
        AWTEvent.ACTION_EVENT_MASK |
        AWTEvent.ADJUSTMENT_EVENT_MASK |
        AWTEvent.ITEM_EVENT_MASK |
        AWTEvent.TEXT_EVENT_MASK;

        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
            @Override
            public void eventDispatched(AWTEvent event) {
                int id = event.getID();
                Object source = event.getSource();
                if (id == 101 && source instanceof JToolTip) {
                    JToolTip tooltip = (JToolTip)source;

                    //System.out.println("" + event.getID() + " " + event.getSource());

                }

            }
        }, mask);
        f.add(l);
        f.setVisible(true);
    }

这篇关于将工具提示添加到JTextPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆