In a elipse-rcp application I am setting the background color for a row in a jface table but I don't want the selection to change this color. I want to be able to specify the color change for a selected row.
在elipse-rcp应用程序中,我在jface表中为行设置背景颜色,但我不希望选择更改此颜色。我希望能够指定所选行的颜色更改。
2 个解决方案
#1
According to this thread, for JFace Viewers
(ListViewer
, Table
, Tree
) by means of using EraseItem
and MeasureItem
events
根据这个帖子,对于JFace Viewers(ListViewer,Table,Tree),通过使用EraseItem和MeasureItem事件
General principle detailed in the article "Custom Drawing Table and Tree Items"
“自定义绘图表和树项”一文中详述的一般原则
SWT.EraseItem
: allows a client to custom draw a cell's background and/or selection, and to influence whether the cell's foreground should be drawnSWT.EraseItem:允许客户端自定义绘制单元格的背景和/或选择,并影响是否应绘制单元格的前景
#2
table.addListener(SWT.EraseItem, new Listener() {
public void handleEvent(Event event) {
event.detail &= ~SWT.HOT;
if ((event.detail & SWT.SELECTED) == 0) return; /// item not selected
Table table =(Table)event.widget;
TableItem item =(TableItem)event.item;
int clientWidth = table.getClientArea().width;
GC gc = event.gc;
Color oldForeground = gc.getForeground();
Color oldBackground = gc.getBackground();
gc.setBackground(colorBackground);
gc.setForeground(colorForeground);
gc.fillRectangle(0, event.y, clientWidth, event.height);
gc.setForeground(oldForeground);
gc.setBackground(oldBackground);
event.detail &= ~SWT.SELECTED;
}
});
#1
According to this thread, for JFace Viewers
(ListViewer
, Table
, Tree
) by means of using EraseItem
and MeasureItem
events
根据这个帖子,对于JFace Viewers(ListViewer,Table,Tree),通过使用EraseItem和MeasureItem事件
General principle detailed in the article "Custom Drawing Table and Tree Items"
“自定义绘图表和树项”一文中详述的一般原则
SWT.EraseItem
: allows a client to custom draw a cell's background and/or selection, and to influence whether the cell's foreground should be drawnSWT.EraseItem:允许客户端自定义绘制单元格的背景和/或选择,并影响是否应绘制单元格的前景
#2
table.addListener(SWT.EraseItem, new Listener() {
public void handleEvent(Event event) {
event.detail &= ~SWT.HOT;
if ((event.detail & SWT.SELECTED) == 0) return; /// item not selected
Table table =(Table)event.widget;
TableItem item =(TableItem)event.item;
int clientWidth = table.getClientArea().width;
GC gc = event.gc;
Color oldForeground = gc.getForeground();
Color oldBackground = gc.getBackground();
gc.setBackground(colorBackground);
gc.setForeground(colorForeground);
gc.fillRectangle(0, event.y, clientWidth, event.height);
gc.setForeground(oldForeground);
gc.setBackground(oldBackground);
event.detail &= ~SWT.SELECTED;
}
});