【问题标题】:How do I change the background selection color for a jface table如何更改 jface 表的背景选择颜色
【发布时间】:2015-11-18 20:15:35
【问题描述】:

在一个 elipse-rcp 应用程序中,我正在为 jface 表中的一行设置背景颜色,但我不希望选择更改此颜色。我希望能够为选定的行指定颜色变化。

【问题讨论】:

    标签: java eclipse-rcp jface


    【解决方案1】:

    根据this thread,对于JFace ViewersListViewerTableTree)通过使用EraseItemMeasureItem事件的方式

    一般原则详见“Custom Drawing Table and Tree Items”一文

    SWT.EraseItem:允许客户端自定义绘制单元格的背景和/或选择,并影响是否应绘制单元格的前景

    【讨论】:

    • 感谢 VonC。使用上面示例中的代码,我可以做我想做的事。
    【解决方案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;
        }
    });
    

    【讨论】:

    • @ks:感谢您的反馈。 +1。如果需要,您可以选择自己的帖子作为官方答案
    猜你喜欢
    • 2021-02-06
    • 1970-01-01
    • 2018-03-04
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2021-01-01
    相关资源
    最近更新 更多