【发布时间】:2014-11-29 15:07:14
【问题描述】:
我这辈子都不知道如何让 SWT 单选按钮单元格使用当前的底层机制来确定表格中的“交替行”颜色和“选定行”颜色。
表格(默认情况下?)已经有交替/选定的行颜色工作并且所有其他列/行正确显示它们。注意 - 我一直在研究代码,但无法弄清楚备用/选定行颜色是如何设置的。有人会认为它是 ColumnLabelProvider,但事实并非如此。我在下面的链接中看到了如何手动设置交替颜色的示例,但我想避免这样做,因为我不会使用已经在后台使用的任何架构。
下面提供了一个代码示例。任何帮助将不胜感激!
private Map<Object, Button> buttons = new HashMap<>();
...
private void createColumns(final TableViewer viewer) {
final TableViewerColumn column0 = new TableViewerColumn(viewer, SWT.CENTER);
column0.getColumn().setWidth(50);
column0.getColumn().setText("MyColumnTitle");
column0.setLabelProvider(new ColumnLabelProvider() {
@Override
public void update(final ViewerCell cell) {
cell.setImage(null);
cell.setText(null);
Button button;
if (buttons.containsKey(cell.getElement())) {
button = buttons.get(cell.getElement());
} else {
final TableItem item = (TableItem) cell.getItem();
button = new Button((Composite) cell.getViewerRow().getControl(), SWT.RADIO);
buttons.put(cell.getElement(), button);
// Make the radio button clickable
final TableEditor editor = new TableEditor(item.getParent());
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
editor.minimumWidth = 50;
editor.setEditor(button, item, cell.getColumnIndex());
editor.layout();
}
// Some things I have tried without success
// button.setBackground(null);
// button.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
// cell.getViewerRow().getControl().getParent().setBackgroundMode(SWT.INHERIT_NONE);
// cell.setBackground(cell.getNeighbor(ViewerCell.RIGHT, true).getBackground());
// cell.setForeground(cell.getNeighbor(ViewerCell.RIGHT, true).getForeground());
}
});
// This is a column that works fine (shows alternating rows/highlighting as expected)
final TableViewerColumn column = new TableViewerColumn(viewer, SWT.LEFT);
column.getColumn().setWidth(240);
column.getColumn().setText("ColumnName");
column.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(final Object element) {
if (element instanceof MyObject) {
return MyObject.getText();
}
return super.getText(element);
}
});
【问题讨论】:
-
我首先会转储 JFace 并从纯 SWT 开始。这将帮助您更好地了解事物是如何联系在一起的。
-
不幸的是,这是不可行的,因为这是使用 JFace 的更大项目的一部分
-
但是您可以先尝试纯 SWT,如果您在那里找到了解决方案,请将其应用于 JFace。您正在运行什么操作系统?