java swt 提示,如何在Java SWT中为TableViewer单元添加工具提示

在Eclipse RCP应用中,当你希望在表格单元格上悬停鼠标时显示详细信息,可以使用ColumnViewerToolTipSupport。你需要为TableViewer启用提示功能,并确保你的标签提供者是从CellLabelProvider派生的。通过覆盖getToolTipText方法,你可以返回要显示的提示文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a table 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 displaied 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);

}

});

解决方案

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'

}

});

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值