Stephane Grenier
Stephane Grenier

Reputation: 15917

JTooltip in JTable on Mac in Java does not select the row when clicked on

On Windows if you click on the table tooltip (JToolTip), it will remove the tooltip AND select the row in the table. However on the Mac, it seems to just remove the tooltip and does NOT select the row in the table.

The code just uses a standard JTable and overrides getToolTipText method from JTable. No custom JToolTip is created or anything like that.

Code:

public class MyJTable extends JTable
{
    @Override
    public String getToolTipText(MouseEvent event)
    {
        return "Hello world";
    }
}

**Update: On further investigation the issue seems to be due to something similar to this bug report. Basically the ToolTipManager.showTipWindow() is creating the tooltip as a HeavyWeight component on the Mac and a LightWeight component on Windows, which then causes the mouselistener not to be fired. The worse part is that regardless if you set the popupType to be lightweight, it will still create it as a heavyweight component in that method when the Java code calls popupFactory.getPopup(...)

Upvotes: 0

Views: 167

Answers (2)

Stephane Grenier
Stephane Grenier

Reputation: 15917

The reason clicking on the ToolTip does not generate a MouseEvent on the JTable is because the ToolTip is generated as a HEAVY_WEIGHT_POPUP rather than a LIGHT_WEIGHT_POPUP.

This can be due to a number of reasons, even something as simple as a L&F that does this.

The way it works in Java Swing is that the PopupFactory will create the ToolTip. In Java mixing HeavyWeight and LightWeight component can have some ramifications and as a result around Java 5-6 (not sure which) the Java language added the ability to define this property, however some L&F libraries don't use it or correctly adhere to it. And if you're using an older version it may just not be possible. For more details on the need for this toggle you can read the following bug report before the toggle was added to Swing.

In any case, if the component (in this case the ToolTip) is a HEAVY_WEIGHT_POPUP then the MouseEvent will not propagate to the JTable, and hence only the ToolTip will disappear and the row in the table will not be selected. The ToolTip needs to be a LIGHT_WEIGHT_POPUP for the MouseEvent to propagate to the JTable.

Upvotes: 1

camickr
camickr

Reputation: 324088

No idea how to fix it.

Normally a different tooltip is given for each row or cell and not the entire table. So as you move the mouse the tooltip changes and is displayed in a different location so you never get an opportunity to actually click on the tooltip.

So, maybe as an alternative approach, you could also override the getToolTipLocation() method:

public Point getToolTipLocation(MouseEvent e)
{
    return new Point(e.getX(), e.getY() + 10);
}

Now the user will never be able to click on it.

Upvotes: 1

Related Questions