gpt4 book ai didi

java - 向 Eclipse 表添加删除图标列

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:59:15 25 4
gpt4 key购买 nike

我目前已经实现了一个 TableTableEditor在我的 Eclipse 插件中支持使用键盘支持的单元级编辑(使用编辑器遍历单元格)。

我还需要一种删除行的方法,我不想采用在表格旁边添加删除按钮的做法,因为它需要点击 2 次才能删除一行(1 次选择一行,1 次删除它)。相反,我想要一个单独的列,其中填充了删除图标。我想到了两种方法来实现这一点,但都遇到了问题:

  1. Table中添加另一列,将图标设置为TableItem.setImage() . 这种方法存在多个问题,您可以在下面看到它们:

    • 选择一行时,图标也会被选中
    • 将鼠标悬停在图标上时,它会得到一个显然无法禁用的图像的工具提示
    • 似乎无法在单元格内将图像垂直居中

    Delete column approach #1

  2. 添加 ScrolledComposite在表格旁边并用删除图标填充它。这听起来有点疯狂,但实际上我已经用这个做了很多。这个想法是用删除图标填充 ScrolledComposite,强制它随着表格的滚动条滚动,并在单击图标时删除相应的行。我只遇到过这种方法的一个阻塞问题:

    • 似乎无法隐藏滚动条

    Delete column approach #2

所以我的问题是:

  • 我如何解决上述任一方法中提到的问题?
  • 还有其他更好的方法吗?

最佳答案

我为第二种方法找到了隐藏滚动条的方法。基本上您需要做的就是:

// 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);
}

结果:

Delete column image

关于java - 向 Eclipse 表添加删除图标列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9124008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com