JFace TableViewer shows two tooltips - custom and

2019-06-24 15:24发布

I got a strange situation here where I need help.

I create a fairly simple JFace Table using a TableViewer. When you do nothing more, you can see the following behavior: when a cell's text is too large, a tooltip showing the complete text will appear after a short wait. That's normal behavior and I guess that the native controls draw these tooltips.

Now I added my own custom tooltips, using

ColumnViewerToolTipSupport.enableFor( tableViewer, ToolTip.NO_RECREATE );

And delivering a (different) text using getToolTipText() method of a label provider.

That works - almost. I got one problem: sometimes (not all the times!) first, the native tooltip appears and shortly thereafter my custom tooltip is drawn on top of it, like shown in this screenshot:

Screenshot showing two tooltips

I have no idea how to effectively disable the native tooltips each cell wants to draw. It's ugly as hell the way it is...

Any clues for me?

3条回答
不美不萌又怎样
2楼-- · 2019-06-24 16:01

I can't tell for sure from your screen shot, but I don't think that what you're seeing is actually two tool tips.

You are seeing one tool tip (your custom one).

The other "popup" you're seeing is the "this table cell isn't wide enough to display its full contents, so we're going to show you the full-blown text that we would be displaying if we did have enough room to show it all" feature of the table itself.

I'd offer up the notion that this is actually the desired behavior. Allowing the user to continue to "peek" at the full contents of the cell, while also providing your custom tool tip (which I'm sure conveys additional/different info than the cell text itself), gives the user everything they might want.

If the overlap bothers you (and I agree that it's unpleasant), you could scoot your custom tool tip out of the way a bit by overriding CellLabelProvider.getToolTipShift().

If you want to disable the "cell too narrow" helper-popup, I'm afraid I don't have any advice for you; I poked around for a while but couldn't figure out how to suppress it. It may not even be possible to do so.

查看更多
甜甜的少女心
3楼-- · 2019-06-24 16:02

I'd like to add to @Jörg's answer:

After setting the table's tool tip to the empty string, I still saw two tooltips occasionally with a multi-column table where some columns had tooltips and some had not.

The reason therefore was, that the CellLabelProviders for columns without tooltips returned null from their getToolTipText() methods (the default) and false from their useNativeToolTip() (also the default). This, in turn, caused the table's tooltip to be reset to null. (see line 174 in ColumnViewerToolTipSupport).

My first attempt was to let the label providers return an empty string from getToolTipText(). Though this fixed the duplicate tooltip problem, an empty custom tooltip was now shown (sigh). But letting useNativeToolTip() return true finally solved the problem.

Well, almost, if the mouse pointer is moved from a column without tooltip to a column with a tooltip, the tooltip is not shown. See this post for a workaround: Native tooltips on JFace TableViewer not working flawlessly

If, for whatever reason, non-native tooltips are required, make sure to return true for useNativeToolTip() when getToolTipText() returns an empty string.

The notes taken here apply to Windows 7, other platforms may behave differently.

查看更多
该账号已被封号
4楼-- · 2019-06-24 16:03

You can effectively disable the native table tooltip like this:

viewer.getTable().setToolTipText("");

See also...

for reference.

查看更多
登录 后发表回答