【问题标题】:How to change TableView frame color in JavaFX?如何在 JavaFX 中更改 TableView 框架颜色?
【发布时间】:2017-01-06 15:00:10
【问题描述】:

我找不到任何有关如何在 TableView 元素具有 焦点 时更改其框架颜色(浅蓝色)的信息。这里有人有什么想法吗?

【问题讨论】:

    标签: java css javafx tableview


    【解决方案1】:

    焦点边框颜色由-fx-focus-color-fx-faint-focus-color两个CSS属性控制。

    在大多数情况下,使用-fx-focus-color 就足够了,例如将此类添加到您的 CSS 文件中:

    .table-view {
        -fx-focus-color: red;
    }
    

    或直接在Java代码中:

    table.setStyle("-fx-focus-color: red;");
    

    但你也可以像这样覆盖两者:

    .table-view {
        -fx-focus-color: red;
        -fx-faint-focus-color: white;
    }
    

    背景:

    通过检查默认样式表modena.css

    .table-view:focused{
        -fx-background-color: -fx-faint-focus-color, -fx-focus-color, -fx-control-inner-background; 
        -fx-background-insets: -1.4, -0.3, 1;
        -fx-background-radius: 2, 0, 0;
    }
    

    发现这些颜色被用作:focused 伪类中的各种背景插入的背景颜色。因此对这个伪类的修改会影响焦点边框(例如边框的宽度)。

    注意1:如果要完全去掉高亮,可以将这些属性设置为-fx-focus-color: transparent;

    注意 2:要对每个 Node 应用相同的高亮颜色,您可以在 CSS 文件中使用 .root { -fx-focus-color: red; } 类。

    【讨论】:

    • 嗨 DVarga,我设置了.root { -fx-focus-color: red; }。但仅对于 TableView 仍不显示红色。其他节点如 Button、TextField 等都是红色的。有什么想法吗?
    • 嗯,这很有趣,因为这是“标准”解决方案。您是否设置了其他 CSS 类?另外,您可以尝试设置-fx-faint-focus-color 属性吗?
    • 您好,我刚刚检查过。.table-view { -fx-background-insets: -3, 1, 2, 1; } 造成了麻烦。当我将其注释掉时,框架颜色变红了。但我真的需要设置彩色框架的厚度。你知道有什么其他方法可以让两个更厚的框架都变成红色吗?
    • 您可以尝试添加例如:.table-view:focused{ -fx-background-insets: -3, -2, 1; },因为在这个伪类中使用了这两个属性(您也可以在这个类中随意使用插图)。
    • 是的!谢谢你,.table-view:focused { -fx-faint-focus-color: red; -fx-background-insets: -3, 1, 2, 1; } 工作得很好!
    【解决方案2】:

    解决方案

    感谢DVarga,我能够想出这个解决方案:

    不知何故,-fx-focus-color: 无法与 -fx-background-insets: 组合使用 TableView 组件。

    但是-fx-background-insets:-fx-faint-focus-color: 一起使用。因此,我的 css 解决方案如下所示:

    .table-view:focused {
        -fx-faint-focus-color: red;
        -fx-background-insets: -3, 1, 2, 1;
    }
    

    现在它可以完美运行了!希望我们能帮助未来的你。

    【讨论】:

      猜你喜欢
      • 2017-07-23
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 2013-11-01
      • 2015-12-04
      • 2019-01-17
      • 1970-01-01
      • 2017-11-24
      相关资源
      最近更新 更多