焦点丢失时突出显示swt表行 [英] Highlighting swt table row on focus lost
问题描述
我正面临同样的问题, SWT:表格丢失选择。我正在使用ubuntu 12.04 NOT windows。即使在焦点丢失之后,是否有任何方法来突出显示SWT表的选定行。我尝试添加焦点监听器到表和焦点丢失我更改了所选项目的背景颜色和聚焦增益重置背景颜色。请参阅代码。
I am facing the same problem mentioned here SWT: Table lost selection . I am using ubuntu 12.04 NOT windows. Is there any way to highlight the selected row of a SWT table even after focus lost. I tried adding focus listener to the table and in focus lost I changed the selected item background colour and on focus gain resets the background colour. See the code.
@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());
}
但它不工作。有没有其他解决方案/解决方法?
But it is not working. Is there any other solution/workaround for this?
推荐答案
可以使用以下代码片段:
The below snippet can be used:
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
}
}
}
});
它对我来说很正常,但没有在较大的数据上测试。
Its is working fine for me but not tested on larger data.
这篇关于焦点丢失时突出显示swt表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!