我面临同样的问题SWT: Table lost selection。我使用的是ubuntu 12.04 NOT windows。有没有办法在焦点丢失后突出显示SWT表的选定行。我尝试将焦点监听器添加到表中并且在焦点丢失时我更改了所选项目背景颜色,并且在焦点增益上重置了背景颜色。见代码。
@Override
public void focusLost(FocusEvent e) {
System.out.println("table focus los");
TableItem item = fileListTable
.getItem(fileListTable.getSelectionIndex());
prevSelItemBackground = item.getBackground();
item.setBackground(soureWindow.getSelectionBackground());//Some random colour
System.out.println(fileListTable.getSelectionIndex());
}
@Override
public void focusGained(FocusEvent e) {
System.out.println("table focus gain");
TableItem item = fileListTable
.getItem(fileListTable.getSelectionIndex());
item.setBackground(prevSelItemBackground);
System.out.println(fileListTable.getSelectionIndex());
}
但它不起作用。有没有其他解决方案/解决方法呢?
答案 0 :(得分:0)
可以使用以下代码段:
this.fileListTable.addSelectionListener(new SelectionListener() {
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// Nothing to do
}
@Override
public void widgetSelected(SelectionEvent e) {
int selectionIndex = fileListTable.getSelectionIndex();
TableItem[] items = fileListTable.getItems();
TableItem newItem;
for (int i = 0; i < items.length; i++) {
String first = items[i].getText(0);
String second = items[i].getText(1);
String third = items[i].getText(2);
items[i].dispose();
newItem = new TableItem(fileListTable, SWT.NONE);
newItem.setText(new String[] { first, second, third });
if (i == selectionIndex) {
newItem.setForeground(soureWindow.getSelectionForeground());//Or Anyother color
newItem.setBackground(soureWindow.getSelectionBackground());//Or Anyother color
} else {
newItem.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));//Default foreground color
newItem.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));//Default background color
}
}
}
});
它对我来说很好,但没有测试更大的数据。