【问题标题】:Dynamically Update Tooltip Currently Displayed动态更新当前显示的工具提示
【发布时间】:2012-10-01 03:06:25
【问题描述】:

我正在尝试获取显示任务当前进度的工具提示。所以我希望工具提示文本在 显示工具提示时更改。但是,当我调用setToolTipText() 时,显示的文本保持不变,直到我将鼠标从工具提示组件中退出并再次输入。之前致电setToolTipText(null) 不会改变任何东西。

【问题讨论】:

    标签: java swing dynamic tooltip


    【解决方案1】:

    实际上,它不会自行更新,即使在两次调用之间将工具提示重置为 null。

    到目前为止,我发现的唯一技巧是模拟鼠标移动事件并将其转发到 TooltipManager。这让他认为鼠标已经移动,工具提示必须重新定位。不漂亮,但效率很高。

    看看这个演示代码,它以 % 显示从 0 到 100 的进度:

    import java.awt.MouseInfo;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import javax.swing.ToolTipManager;
    
    public class TestTooltips {
    
        protected static void initUI() {
            JFrame frame = new JFrame("test");
            final JLabel label = new JLabel("Label text");
            frame.add(label);
            frame.pack();
            frame.setVisible(true);
            Timer t = new Timer(1000, new ActionListener() {
    
                int progress = 0;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (progress > 100) {
                        progress = 0;
                    }
                    label.setToolTipText("Progress: " + progress + " %");
                    Point locationOnScreen = MouseInfo.getPointerInfo().getLocation();
                    Point locationOnComponent = new Point(locationOnScreen);
                    SwingUtilities.convertPointFromScreen(locationOnComponent, label);
                    if (label.contains(locationOnComponent)) {
                        ToolTipManager.sharedInstance().mouseMoved(
                                new MouseEvent(label, -1, System.currentTimeMillis(), 0, locationOnComponent.x, locationOnComponent.y,
                                        locationOnScreen.x, locationOnScreen.y, 0, false, 0));
                    }
                    progress++;
                }
            });
            t.start();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    initUI();
                }
            });
        }
    }
    

    【讨论】:

    • 只是一个小问题。工具提示的初始延迟必须
    【解决方案2】:

    这是Guillaume Polet 的答案的简化版本,它在单一方法中是自包含的。此代码假定之前已调用component.setToolTip("...");。此代码显示如何定期更新工具提示以显示进度。

    public static void showToolTip(JComponent component)
    {
       ToolTipManager manager;
       MouseEvent event;
       Point point;
       String message;
       JComponent component;
       long time;
    
       manager = ToolTipManager.sharedInstance();
       time    = System.currentTimeMillis() - manager.getInitialDelay() + 1;  // So that the tooltip will trigger immediately
       point   = component.getLocationOnScreen();
       event   = new MouseEvent(component, -1, time, 0, 0, 0, point.x, point.y, 1, false, 0);
    
       ToolTipManager.
          sharedInstance().
          mouseMoved(event);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 2019-01-07
      相关资源
      最近更新 更多