I have enabled tooltips in my JTable by overriding the JComponent method that the JTable inherits:
public String getToolTipText(MouseEvent e) { ... }
Now, suppose a user hovers over a cell, the tooltip appears, and then (s)he starts editing the cell, I want to forcefully dismiss the tooltip.
Currently, the tooltip just hangs around until the value that I specified using ToolTipManager#setDismissDelay expires. The tooltip can sometimes obscure the view of the cell being edited which is why I want to dismiss it the moment any cell on the table goes into edit mode.
I tried the following approach (this is pseudo-code)
public String getToolTipText(MouseEvent e)
{
    if(table-is-editing)
        return null;
    else
        return a-string-to-display-in-the-tooltip;
}
Of course, this only had the effect of NOT showing tooltips of the table is ALREADY in edit mode. I knew this wouldn't work, but it was more of a shot in the dark.
You can show/hide a tooltip by using code like:
//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 );
}
You couuld probably override the prepareCellEditor(...) method of JTable to add this code and it should hide any tooltip before displaying the editor.
Edit:
In response to Kleopatra's comment I then add the following to make sure the Action is added to the ActionMap:
table.getInputMap().put(KeyStroke.getKeyStroke("pressed F2"), "dummy");
ToolTipManager.sharedInstance().registerComponent( table );
Was about to comment "something wrong with your" - but remembered a use case when not hiding the Tooltip on starting edits may happen :-)
Some facts:
So it requires a handful of tweaks to implement the required behaviour, below is a code snippet (note to myself: implement in SwingX :-)
JTable table = new JTable(new AncientSwingTeam()) {
    {
        // force the TooltipManager to install the hide action
        getInputMap().put(KeyStroke.getKeyStroke("ctrl A"), 
             "just some dummy binding in the focused InputMap");
        ToolTipManager.sharedInstance().registerComponent(this);
    }
    @Override
    public boolean editCellAt(int row, int column, EventObject e) {
        boolean editing = super.editCellAt(row, column, e);
        if (editing && hasFocus()) {
            hideToolTip();
        }
        return editing;
    }
    private void hideToolTip() {
        Action action = getActionMap().get("hideTip");
        if (action != null) {
            action.actionPerformed(new ActionEvent(
                this, ActionEvent.ACTION_PERFORMED, "myName"));
        }
    }
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With