是否可以在 JavaFX 中将 StringProperty 绑定到 POJO 的字符串? [英] Is it possible to bind a StringProperty to a POJO's String in JavaFX?

查看:23
本文介绍了是否可以在 JavaFX 中将 StringProperty 绑定到 POJO 的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JavaFX 2 创建应用程序.我打算将 UI 与数据和逻辑隔离.考虑到这一点,我有很多这样的数据对象:

I'm creating an application using JavaFX 2. I intend to isolate the UI from data and logics. Considering that, I have a lot of data objects like this:

public class Entity {
    private String m_Value;
    public Entity(String value) { m_Value = value; }
    public String getValue() { return m_Value; }
    public void setValue(String value) { m_Value = value; }
}

例如,我很难在 m_Value 属性和标签的 textProperty 之间创建绑定.

I'm having a hard time creating a binding between the m_Value attribute and the textProperty of a Label, for instance.

这里的另一个类似问题建议人们使用 JFXtras,但是我不确定我是否可以使用它(公司限制).所以我试图找到一些替代方案.

Another similar question here suggested people to use JFXtras, however I'm not sure I'm allowed to use that (company restrictions). So I'm trying to find some alternative to that.

我的想法是使用对象来表示我的数据实体的所有属性,而不是原始类型(int 将使用 Integer 等).这样,我就可以利用 java 的传递引用模型.然后,在 UI 域中,​​我可以创建一个引用此属性的属性.但我不确定这是否可能.

My idea would be to use objects to represent all attributes of my data entities, instead of primitive types (int would use Integer and so on). That way, I can take advantage of the pass by reference model of java. Then, in the UI domain, I could create a Property referencing this attribute. But I'm not sure whether this is possible or not.

是否可以使用 ObjectProperty 类来解决这个问题?

It it possible to solve this problem using the ObjectProperty class?

另一种替代方法是使用 JavaBeanProperty 系列类,例如 JavaBeanStringPropertyBuilder、JavaBeanIntegerPropertyBuilder.可能吗?

Another alternative could be using the JavaBeanProperty family of classes, like JavaBeanStringPropertyBuilder, JavaBeanIntegerPropertyBuilder. Is it possible?

这两种方法我都试过了,但恐怕我在 JavaFX 方面还没有那么丰富的经验来解决这个问题.有人可以帮忙吗?

I've tried both ways, but I'm affraid I'm not that experienced in JavaFX yet to solve this. Can anyone help?

我不想在我的数据对象中使用 StringProperty 或 IntegerProperty,因为我不想在这个包中包含任何 JavaFX 依赖项.此应用程序很有可能成为 Eclipse 插件,并且 JavaFX 可能会关闭.这样我以后就可以避免过多的返工.

I don't want to use StringProperty or IntegerProperty inside my data objects because I don't want any JavaFX dependencies in this package. There is a strong possibility this application becomes an Eclipse plugin and probably JavaFX would be off. This way I can avoid too much rework in the future.

非常感谢.

推荐答案

您显然可以在 Entity 中设置值,除了在 JavaFX 中使用侦听器外,无需执行任何操作.如果你想走另一条路,你可以添加 java.beans.PropertyChangeSupport. 它不在 javafx 包中.

You can obviously set the values in Entity without doing anything except using a listener in JavaFX. If you want to go the other way you can add java.beans.PropertyChangeSupport. It's not in a javafx package.

package bindpojo;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class BindPojo extends Application {
    StringProperty fxString = new SimpleStringProperty();
    StringProperty tmpString = new SimpleStringProperty();

    @Override
    public void start(Stage primaryStage) {
        VBox vbox = new VBox();
        TextField text = new TextField();
        Label label1 = new Label();
        Label label2 = new Label();
        Entity entity = new Entity("");
        vbox.getChildren().addAll(text,label1,label2);
        Scene scene = new Scene(vbox, 300, 200);
        primaryStage.setScene(scene);
        primaryStage.show();

        fxString.bindBidirectional(text.textProperty());

        fxString.addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                entity.setValue(newValue);
                label1.setText(entity.getValue());
            }
        });

        entity.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                tmpString.set(evt.getNewValue().toString());
            }
        });

        label2.textProperty().bind(tmpString);
    }

    public class Entity {
    private String m_Value;
    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    public Entity(String value) { m_Value = value; }
    public String getValue() { return m_Value; }
    public void setValue(String value) {
        pcs.firePropertyChange("m_Value", m_Value, value);
        m_Value = value;
    }
    public void addPropertyChangeListener(PropertyChangeListener listener) {
                    pcs.addPropertyChangeListener(listener);
                }
    }
}

您实际上并不需要 tmpString,我只是在您希望 Entity 类似于 javafx 字符串属性的情况下使用它.您可以只在属性更改侦听器中设置标签,而不是设置字符串属性然后绑定到它.

You don't really need the tmpString, I just used used it in case you want to have Entity resemble a javafx string property. You could just set the label in the property change listener instead of setting a string property and then binding to it.

这篇关于是否可以在 JavaFX 中将 StringProperty 绑定到 POJO 的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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