将鼠标悬停在树的整行上

Hover color for entire row of the tree

提问人:Satya 提问时间:10/12/2023 最后编辑:Satya 更新时间:10/12/2023 访问量:34

问:

我有一个 swt 树,我为其添加了 SWT。EraseItem 侦听器为了获取选择,将鼠标悬停在树项的颜色上。选择,悬停颜色显示正确,但该颜色仅应用于文本。我希望它应用于整行。

截图供您参考:Screenshot for your reference

tree.addListener(SWT.EraseItem, new Listener() {

        @Override
        public void handleEvent(Event event) {
            if ((event.detail & SWT.SELECTED) != 0 && (event.detail & SWT.MouseHover) != 0) {

                int width = getBounds().width;
                GC gc = event.gc;
                gc.setForeground(new Color(Display.getCurrent(), 0, 0, 0));
                gc.setBackground(new Color(Display.getCurrent(), 213, 215, 219));
                gc.fillRectangle(0, event.y, width, event.height);

                event.detail &= ~SWT.MouseHover;
                event.detail &= ~SWT.SELECTED;
            }
            if ((event.detail & SWT.SELECTED) != 0 && !tree.isFocusControl()) {
                int width = tree.getClientArea().width;
                GC gc = event.gc;
                gc.setForeground(new Color(Display.getCurrent(), 0, 0, 0));
                gc.setBackground(new Color(Display.getCurrent(), 230, 232, 235));
                gc.fillRectangle(0, event.y, width, event.height);
                event.detail &= ~SWT.SELECTED;
            }
            if ((event.detail & SWT.MouseHover) != 0) {
                int width = tree.getClientArea().width;
                GC gc = event.gc;
                gc.setForeground(new Color(Display.getCurrent(), 0, 0, 0));
                gc.setBackground(new Color(Display.getCurrent(), 213, 215, 219));
                gc.fillRectangle(0, event.y, width, event.height);
                event.detail &= ~SWT.MouseHover;
            }

            if ((event.detail & SWT.SELECTED) != 0) {
                int width = tree.getClientArea().width;
                GC gc = event.gc;
                gc.setForeground(new Color(Display.getCurrent(), 0, 0, 0));
                gc.setBackground(new Color(Display.getCurrent(), 192, 235, 237));
                gc.fillRectangle(0, event.y, width, event.height);
                event.detail &= ~SWT.SELECTED;
            }

            Color previousForegroundColor = event.gc.getForeground();
            event.gc.setForeground(previousForegroundColor);
        }
    });
Java SWT 选择 线宽

评论

0赞 greg-449 10/12/2023
对不起,但我不明白这个问题。你的意思是一直向右延伸吗?EraseItem 事件应将边界设置为列的全宽。向我们展示您的代码。
0赞 Satya 10/12/2023
是的,我希望这个悬停颜色向右扩展以覆盖整棵树 我已经添加了上面的代码,请看一下
0赞 greg-449 10/12/2023
什么?为什么这与其他测试不同?在快速测试中,macOS 树似乎不会在树上生成 MouseHover 事件,所以我无法真正测试这一点。getBounds()
0赞 Satya 10/12/2023
任何其他方法可以实现树整行的悬停颜色,您能否提供代码
0赞 greg-449 10/12/2023
注意:实际上是事件 ID,而不是详细信息字段中的值。您应该使用 - 它们具有相同的数值,因此它不会改变代码的工作方式,但您应该使用正确的值。由于我只使用似乎不会为树生成 HOT 事件的 macOS,因此我无法测试或提出任何建议。SWT.MouseHoverSWT.HOT

答: 暂无答案