【问题标题】:SWT table cell foreground on selection选择时的 SWT 表格单元格前景
【发布时间】:2014-01-16 15:25:12
【问题描述】:

我有一个表格,我使用自己的自定义 LabelProvider 来显示背景和前景色。

this 我得出我无法更改选择背景颜色。因此,我希望能够在选择时更改文本的前景色。但是我不知道如何检测是否选择了特定行,以便我可以提供不同的前景色。

任何帮助将不胜感激,我对 swt 不太精通。

编辑: 对于任何搜索这就是我所做的人

public static void attachListenerIfWin7(Table table)
{
    if (System.getProperty("os.name").startsWith("Windows") && System.getProperty("os.version").contains("6.1"))
    {
        table.addListener(SWT.EraseItem, new Listener()
        {
            public void handleEvent(Event event)
            {
                event.detail &= ~SWT.HOT;
                if (event.detail != 24 && event.detail != 22 && event.detail != 18)
                    return;
                int clientWidth = ((Composite) event.widget).getClientArea().width;
                GC gc = event.gc;
                Color oldForeground = gc.getForeground();
                Color oldBackground = gc.getBackground();
// hover
                if (event.detail == 24)
                {
                    gc.setBackground(new Color(event.display, new RGB(115, 115, 115)));
                    gc.setForeground(new Color(event.display, new RGB(115, 115, 115)));
                    gc.fillRectangle(0, event.y, clientWidth, event.height);
                }
// selected
                else if (event.detail == 22)
                {
                    gc.setBackground(new Color(event.display, new RGB(37, 37, 37)));
                    gc.setForeground(new Color(event.display, new RGB(105, 105, 105)));
                    gc.fillGradientRectangle(0, event.y, clientWidth, event.height, true);
                }
// selected but out of focus
                else if (event.detail == 18)
                {
                    gc.setBackground(new Color(event.display, new RGB(57, 57, 57)));
                    gc.setForeground(new Color(event.display, new RGB(135, 135, 135)));
                    gc.fillGradientRectangle(0, event.y, clientWidth, event.height, true);
                }
                gc.setForeground(oldForeground);
                gc.setBackground(oldBackground);
                event.detail &= ~SWT.SELECTED;
            }
        });
    }
}

【问题讨论】:

    标签: java swt


    【解决方案1】:

    这是在 SWT 表/树项上设置选择背景的示例代码

      table.addListener(SWT.EraseItem, new Listener() {
         public void handleEvent(Event event) {
            event.detail &= ~SWT.HOT;
            if ((event.detail & SWT.SELECTED) == 0) 
              return; 
            int clientWidth = ((Composite)event.widget).getClientArea().width;
            GC gc = event.gc;
           Color oldForeground = gc.getForeground();
            Color oldBackground = gc.getBackground();
            gc.setBackground(event.display.getColor(SWT.COLOR_YELLOW));
            gc.setForeground(event.display.getColor(SWT.COLOR_BLUE));
            gc.fillGradientRectangle(0, event.y, clientWidth, event.height, true);
            gc.setForeground(oldForeground);
            gc.setBackground(oldBackground);
            event.detail &= ~SWT.SELECTED;
         }
      });
    

    【讨论】:

    • 如果你不介意我问的话。如何防止它改变悬停颜色?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多