【问题标题】:SWT - Table Viewer center checkboxSWT - 表格查看器中心复选框
【发布时间】:2018-08-29 09:39:38
【问题描述】:

我的表格查看器的最后一列仅包含一个复选框。复选框出现在单元格的左侧,并且由于列名很长,因此看起来很难看。如何将复选框居中在单元格的中间?不使用图像可以吗?这是我创建列的方式:

    // third column - check box. temporary
    TableColumn column = new TableColumn(viewer.getTable(), SWT.NONE);
    column.setText("PrettyLongColumnName");
    column.setWidth(100);
    TableViewerColumn checkColumn = new TableViewerColumn(viewer, column);
    checkColumn.setLabelProvider(new ColumnLabelProvider() {
        // the checkboxes should be disposed and rebuilt when input changes
        @Override
        public void update(ViewerCell cell) {
            MyObject system = (MyObject) cell.getElement();
            TableItem item = (TableItem) cell.getItem();
            Button button;
            if (buttonsMap.containsKey(cell.getElement())) {
                button = rightTableButtons.get(cell.getElement());
            } else {
                button = new Button((Composite) cell.getViewerRow().getControl(), SWT.CHECK);
                button.setEnabled(true);                   
                buttonsMap.put(cell.getElement(), button);

                TableEditor editor = new TableEditor(item.getParent());
                editor.grabHorizontal = true;
                editor.grabVertical = true;
                editor.setEditor(button, item, cell.getColumnIndex());
                editor.layout();
                }
            }
        }
    });

【问题讨论】:

  • 我知道的唯一方法是使用图像和 OwnerDrawLabelProvider

标签: java swt jface tableviewer


【解决方案1】:

TL;DR:nativley 不可用,但可以通过史诗黑客实现。

复选框实际上是一种操作系统功能。由于 SWT 是跨平台的,我们依赖于它由操作系统提供。

AFIK 所有操作系统(Gtk/Win32/Cocoa)提供的唯一东西是第一列上的单个复选框。 其他 花哨的功能必须手动实现。

我见过人们这样做的一种方法是绘制自定义图标,然后在使用事件侦听器单击它时更新图标。

如何在右侧绘制图标的一个示例是this snippet。您必须添加单击侦听器才能在单击时将图标更改为选中/未选中。

注意,这可能会导致您的应用程序在不同平台和主题(例如深色主题)之间看起来不一致。 为了解决这个问题,我们让一些人实际生成了一个本地复选框,然后以编程方式截取屏幕截图,然后将其绘制在列的右侧。我认为这是最好的骇客。

如果您有任何问题,请告诉我。

【讨论】:

  • 谢谢老兄。是的,我同意这太过分了。我决定让列名和宽度更小,这样复选框现在看起来还不错。我接受了你的回答,因为我花时间解释它。和平
猜你喜欢
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
相关资源
最近更新 更多