当前位置: 首页 > 知识库问答 >
问题:

选择时SWT表格单元格前景

慕容玉堂
2023-03-14

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

由此我得出,我无法更改选择的背景色。因此,我想能够改变前景色的文字选择后。但是,我不知道如何检测是否选择了特定的行,以便提供不同的前景色。

任何帮助将不胜感激,我不太精通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;
            }
        });
    }
}

共有1个答案

景阳曜
2023-03-14

下面是在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;
     }
  });
 类似资料:
  • 我正在使用swt表,并实现了一个selectionListener: 我点击第一项,听者起作用了! 现在,按下按钮后,我想自动选择表中的下一项。我已经尝试过: 和 选择更改(蓝色),但selectionListener没有反应?! 也许你能帮我解决这个问题。 谢谢你的帮助! 亲切的问候

  • 问题内容: 即将选择单元格时,是否会触发任何事件?有ListSelectionListener,但是它具有仅在选择发生后才触发的事件。我需要某种方法来取消选择事件,并且使用ListSelectionListener并不容易,因为选择已经发生,并且我需要具有一些状态变量来指示选择是正常的还是取消先前的选择。 有没有办法关闭选择通知?但是,这不是100%好的解决方案(如果某些侦听器将选择状态保存在本地

  • 我想用复选框显示所有行,所有列,因为我只想要true/false值。但是我想访问单个单元格值,即每个复选框都可以选中/不选中。见下图。 根据我的知识,当我勾选复选框时,行的所有复选框都将被选中。那么,我可以选中/取消选中单个框吗?

  • 我找到了在点击JTable标题时选择特定列的代码。对于我的模块,如果有人选择一个JTable单元格,则必须删除之前选择的所有列。我成功地更改了

  • table单元格选中,存在合并的单元格时,选中样式和期望的不一样,如何解决这个问题? 不存在单元格合并时不存在这个问题: 存在单元格合并时: 想要的是这种 目前合并实现方案,是获取起始点击的单元格坐标,为选中的每一个单元格坐标添加选中样式。 有无好的解决办法?

  • 问题内容: 我开发了Eclipse RCP应用程序,并且遇到了问题。我们数据库中有一些布尔格式的数据,用户希望使用来查看该字段。 我试图使用来实现它作为 表编辑器 ,但它的工作速度太慢:( 我尝试使用2张图片-选中和未选中的复选框,都可以,但是我无法将它们居中对齐,它们会自动向左对齐。 我什至找到了如何捕获和事件以及如何通过更改字段手动处理它们,但是我遇到了一个问题- 我现在无法测量或绘制哪个列,