我通过覆盖 JTable 继承的 JComponent 方法在我的 JTable 中启用了工具提示:
public String getToolTipText(MouseEvent e) { ... }
现在,假设用户将鼠标悬停在一个单元格上,工具提示出现,然后他开始编辑单元格,我想强行关闭工具提示。
目前,工具提示会一直挂起,直到我使用 ToolTipManager#setDismissDelay 指定的值过期。工具提示有时会掩盖正在编辑的单元格的 View ,这就是为什么我想在表格上的任何单元格进入编辑模式时关闭它。
我尝试了以下方法(这是伪代码)
public String getToolTipText(MouseEvent e)
{
if(table-is-editing)
return null;
else
return a-string-to-display-in-the-tooltip;
}
当然,这只会产生不显示表的工具提示的效果,该表已经处于编辑模式。我知道这行不通,但这更像是在黑暗中尝试。
最佳答案
您可以使用如下代码显示/隐藏工具提示:
//Action toolTipAction = component.getActionMap().get("postTip");
Action toolTipAction = component.getActionMap().get("hideTip");
if (toolTipAction != null)
{
ActionEvent ae = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, "");
toolTipAction.actionPerformed( ae );
}
您可能会覆盖 JTable 的 prepareCellEditor(...)
方法来添加此代码,它应该在显示编辑器之前隐藏所有工具提示。
编辑:
为了回应 Kleopatra 的评论,我添加了以下内容以确保将 Action 添加到 ActionMap 中:
table.getInputMap().put(KeyStroke.getKeyStroke("pressed F2"), "dummy");
ToolTipManager.sharedInstance().registerComponent( table );
关于java - 如何在单元格编辑开始时关闭表格中的所有工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8921261/