How to add a tooltip for TableViewer cell's in Java SWT

别来无恙 提交于 2019-11-30 17:49:01

问题


I have a TableViewer where each row represents different values. The string being kinda long, I would like that when I hover the mouse over the specific cell, a tooltip should pop-up with the information from the cell.

I wrote this code but tooltip is not displayed and getToolTipText method is never executed:

columnMessage.setLabelProvider(new ColumnLabelProvider()
{
    @Override
    public void update(ViewerCell cell)
    {
       // ... as now
    }

    @Override
    public String getToolTipText(Object element)
    {
       return getText(element);
    }
});

回答1:


For TableViewer add a call to enable tool tips with:

ColumnViewerToolTipSupport.enableFor(viewer);

where viewer is your table viewer.

This requires that your label provider(s) for the table are derived from CellLabelProvider (or one of the classes derived from that such as ColumnLabelProvider).

You can then override a number of methods in the label provider to control the tool tips:

public String getToolTipText(Object element)

to return the text.

public Image getToolTipImage(Object object)
public Color getToolTipBackgroundColor(Object object)
public Color getToolTipForegroundColor(Object object)
public Font getToolTipFont(Object object)
public int getToolTipStyle(Object object)

for images, colors, fonts and style

public Point getToolTipShift(Object object)
public int getToolTipTimeDisplayed(Object object)
public int getToolTipDisplayDelayTime(Object object)

to control the tool tip offset, and when it is displayed.

There are defaults for all of these so getToolTipText is the only one you really need to override.

So for your code you might do:

columnMessage.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public void update(ViewerCell cell) {
            ... as now
        }

        @Override
        public String getToolTipText(Object element)
        {
           // TODO return the tool tip text for 'element'
        }

        @Override
        public String getText(Object element)
        {
          // TODO get text from 'element'
        }
    });
}


来源:https://stackoverflow.com/questions/29646389/how-to-add-a-tooltip-for-tableviewer-cells-in-java-swt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!