Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss all tooltips in table when cell editing begins?

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.

like image 278
eternaln00b Avatar asked Dec 10 '22 03:12

eternaln00b


2 Answers

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 );
like image 116
camickr Avatar answered Mar 07 '23 21:03

camickr


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:

  • tooltips are hidden on mouseExit and on focusLost the component which is registered with the ToolTipManager
  • when starting an edit and the editing component gets focus so the tooltip is hidden automatically
  • by default, JTable does not yield focus to the editing component is the editing is started by typing into the cell (as opposed by double-click or F2): in this case no focusLost is fired and consequently the tooltip not hidden
  • the ToolTipManager indeed installs a hideAction which might be re-used (as @camickr mentioned). But - that action is installed only if the component has a inputMap of type WHEN_FOCUSED. Which is not the case for JTable (all its bindings are in WHEN_ANCESTOR)

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"));
        }
    }

};
like image 42
kleopatra Avatar answered Mar 07 '23 23:03

kleopatra