强制出现 Java 工具提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6473464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force a Java Tooltip to Appear
提问by Joseph
Given a JTextField
(or any JComponent
for that matter), how would one go about forcing that component's designated tooltip to appear, without any direct input event from the user? In other words, why is there no JComponent.setTooltipVisible(boolean)
?
给定一个JTextField
(或任何JComponent
一个),如何在没有来自用户的任何直接输入事件的情况下强制显示该组件的指定工具提示?换句话说,为什么没有JComponent.setTooltipVisible(boolean)
?
回答by pstanton
The only way (besides creating your own Tooltip window) I've found is to emmulate the CTRL+F1 keystroke on focus:
我发现的唯一方法(除了创建自己的工具提示窗口)是在焦点上模拟 CTRL+F1 击键:
new FocusAdapter()
{
@Override
public void focusGained(FocusEvent e)
{
try
{
KeyEvent ke = new KeyEvent(e.getComponent(), KeyEvent.KEY_PRESSED,
System.currentTimeMillis(), InputEvent.CTRL_MASK,
KeyEvent.VK_F1, KeyEvent.CHAR_UNDEFINED);
e.getComponent().dispatchEvent(ke);
}
catch (Throwable e1)
{e1.printStackTrace();}
}
}
Unfortunately, the tooltip will disappear as soon as you move your mouse (outside of the component) or after a delay (see ToolTipManager.setDismissDelay
).
不幸的是,只要您移动鼠标(组件外)或延迟(参见 参考资料),工具提示就会消失ToolTipManager.setDismissDelay
。
回答by camickr
You need to invoke the default Action to show the tooltip. For example to show a tooltip when a component gains focus you can add the following FocusListener to the component:
您需要调用默认操作来显示工具提示。例如,要在组件获得焦点时显示工具提示,您可以将以下 FocusListener 添加到组件中:
FocusAdapter focusAdapter = new FocusAdapter()
{
public void focusGained(FocusEvent e)
{
JComponent component = (JComponent)e.getSource();
Action toolTipAction = component.getActionMap().get("postTip");
if (toolTipAction != null)
{
ActionEvent postTip = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, "");
toolTipAction.actionPerformed( postTip );
}
}
};
Edit:
编辑:
The above code doesn't seem to work anymore. Another approach is dispatch a MouseEvent to the component:
上面的代码似乎不再起作用了。另一种方法是将 MouseEvent 分派给组件:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PostTipSSCCE extends JPanel
{
public PostTipSSCCE()
{
FocusAdapter fa = new FocusAdapter()
{
public void focusGained(FocusEvent e)
{
JComponent component = (JComponent)e.getSource();
MouseEvent phantom = new MouseEvent(
component,
MouseEvent.MOUSE_MOVED,
System.currentTimeMillis(),
0,
10,
10,
0,
false);
ToolTipManager.sharedInstance().mouseMoved(phantom);
}
};
JButton button = new JButton("Button");
button.setToolTipText("button tool tip");
button.addFocusListener( fa );
add( button );
JTextField textField = new JTextField(10);
textField.setToolTipText("text field tool tip");
textField.addFocusListener( fa );
add( textField );
JCheckBox checkBox = new JCheckBox("CheckBox");
checkBox.setToolTipText("checkbox tool tip");
checkBox.addFocusListener( fa );
add( checkBox );
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("PostTipSSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane(new PostTipSSCCE()) );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
This approach will result in a slight delay before the tooltip is displayed as it simulated the mouse entering the component. For immediate display of the tooltip you can use pstanton's solution.
这种方法会导致在显示工具提示之前稍微延迟,因为它模拟了鼠标进入组件。要立即显示工具提示,您可以使用 pstanton 的解决方案。
回答by Jan Trembulak
For me works the similar version stated above (instead of Timer I used SwingUtilities and invokeLater approach):
对我来说,使用上述类似版本(而不是 Timer 我使用 SwingUtilities 和 invokeLater 方法):
private void showTooltip(Component component)
{
final ToolTipManager ttm = ToolTipManager.sharedInstance();
final int oldDelay = ttm.getInitialDelay();
ttm.setInitialDelay(0);
ttm.mouseMoved(new MouseEvent(component, 0, 0, 0,
0, 0, // X-Y of the mouse for the tool tip
0, false));
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
ttm.setInitialDelay(oldDelay);
}
});
}
回答by bench_doos
It's not a ToolTip
, but you can use a balloon tip:
http://timmolderez.be/balloontip/doku.php
这不是ToolTip
,但您可以使用气球提示:http:
//timmolderez.be/balloontip/doku.php
It is showing just on call and feels better in some moments then Default ToolTip
它只是随叫随到,在某些时候感觉比默认更好 ToolTip
回答by Olivier Faucheux
You can access the ToolTipManager
, set the initial delay to 0 and send it an event. Don't forget to restore the values afterwards.
您可以访问ToolTipManager
,将初始延迟设置为 0 并向其发送事件。之后不要忘记恢复这些值。
private void displayToolTip()
{
final ToolTipManager ttm = ToolTipManager.sharedInstance();
final MouseEvent event = new MouseEvent(this, 0, 0, 0,
0, 0, // X-Y of the mouse for the tool tip
0, false);
final int oldDelay = ttm.getInitialDelay();
final String oldText = textPane.getToolTipText(event);
textPane.setToolTipText("<html><a href='http://www.google.com'>google</a></html>!");
ttm.setInitialDelay(0);
ttm.setDismissDelay(1000);
ttm.mouseMoved(event);
new Timer().schedule(new TimerTask()
{
@Override
public void run()
{
ttm.setInitialDelay(oldDelay);
textPane.setToolTipText(oldText);
}
}, ttm.getDismissDelay());
}