Java fx绑定样式 [英] Java fx binding styles

查看:368
本文介绍了Java fx绑定样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里的第一篇文章,所以如果我做错了什么或者有什么不清楚,我会提前感到抱歉。

This is my first post here, so I'm sorry in advance if I did anything wrong or anything is unclear.

在javafx中,我有一个包含大约5列的树表视图,第一列只显示对象名称的字符串。在set cell value factory方法中,我只需获取对象的name属性,如下所示。

In javafx I have a tree table view with about 5 columns and the first column simply displays a string for the name of the object. In the set cell value factory method I simply get the name property for my object as shown below.

column.setCellValueFactory(new Callback<CellDataFeatures<Foo, String>, ObservableValue<String>>(){
    @Override
    public ObservableValue<String> call(CellDataFeatures<Foo, String> row) {
        if (row.getValue().getValue() instanceof Foo){
            final Foo bar = (Foo)row.getValue().getValue();
            return bar.getNameProperty();
        }
        return null;
    }
});

我希望能够通过将其绑定到表中所示来更改此字符串的颜色风格属性。我已经尝试将字符串更改为Text对象但无法绑定样式属性。有没有办法可以将样式属性绑定到Foo对象中的某些内容

I want to be able to change the color of this string as shown in the table by binding it to a style property. I have tried changing the string to a Text object but could not bind the style property. Is there a way I could bind the style property to something in the Foo object along the lines of

String style = "-fx-text-fill: red"

这样每当我想以编程方式改变样式时,它将是表中也更新了?

That way whenever I want to programatically change the style, it will be updated in the table as well?

再次,对任何不清楚的事情道歉!

Again, apologies for anything that is unclear!

推荐答案

要影响像样式这样的单元格视觉效果,你需要定义一个cellFactory(除了你的cellValueFactory)。

To affect cell visuals such as style, you need to define a cellFactory (in addition to your cellValueFactory).

一种方法来实现绑定样式属性到Foo对象中的东西是:

One way to accomplish "bind the style property to something in the Foo object" is to:


  1. 添加到Foo定义为StringProperty的styleProperty。

  2. 在单元工厂提供的单元格中,当附加到单元格的foo对象发生更改时,取消绑定单元格的styleProperty,然后将单元格的styleProperty绑定到Foo项目的styleProperty。

然后修改Fo的styleProperty o对象,TreeTable中foo显示的样式将自动同时更改。

Then when you modify the styleProperty of a Foo object, the style of the foo display in your TreeTable will automatically change simultaneously.

这篇关于Java fx绑定样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆