我目前已经实现了一个 Table
用TableEditor
在我的 Eclipse 插件中支持使用键盘支持的单元级编辑(使用编辑器遍历单元格)。
我还需要一种删除行的方法,我不想采用在表格旁边添加删除按钮的做法,因为它需要点击 2 次才能删除一行(1 次选择一行,1 次删除它)。相反,我想要一个单独的列,其中填充了删除图标。我想到了两种方法来实现这一点,但都遇到了问题:
在
Table
中添加另一列,将图标设置为TableItem.setImage()
. 这种方法存在多个问题,您可以在下面看到它们:- 选择一行时,图标也会被选中
- 将鼠标悬停在图标上时,它会得到一个显然无法禁用的图像的工具提示
- 似乎无法在单元格内将图像垂直居中
添加
ScrolledComposite
在表格旁边并用删除图标填充它。这听起来有点疯狂,但实际上我已经用这个做了很多。这个想法是用删除图标填充ScrolledComposite
,强制它随着表格的滚动条滚动,并在单击图标时删除相应的行。我只遇到过这种方法的一个阻塞问题:- 似乎无法隐藏滚动条
- 似乎无法隐藏滚动条
所以我的问题是:
- 我如何解决上述任一方法中提到的问题?
- 还有其他更好的方法吗?
最佳答案
我为第二种方法找到了隐藏滚动条的方法。基本上您需要做的就是:
// ScrolledComposite sc;
sc.setAlwaysShowScrollBars(true);
sc.getVerticalBar().setVisible(false);
然后将 ScrolledComposite
的宽度设置为 1
以摆脱不可见的 ScrollBar
占用的额外空间。
并保持滚动条同步:
// Table table;
// ScrolledComposite sc;
// int tableRowHeight;
protected void createTable() {
...
// Set the listener that dictates the table row height.
table.addListener(SWT.MeasureItem, new Listener() {
@Override
public void handleEvent(Event event) {
event.height = tableRowHeight;
}
});
// Set the listener for keeping the scrollbars in sync.
table.getVerticalBar().addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
syncDeleteColumnScrollBar();
}
});
}
// This is extracted out into a method so it can also be called
// when removing a table row.
protected void syncDeleteColumnScrollBar() {
sc.setOrigin(0, table.getVerticalBar().getSelection() * tableRowHeight);
}
结果:
关于java - 向 Eclipse 表添加删除图标列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9124008/