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

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

问题描述

我正在使用JavaFX创建一个应用程序。
我打算将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属性和m_Value属性之间创建绑定例如,Label的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的pass by reference模型。
然后,在UI域中,我可以创建一个引用此属性的Property。但我不确定这是否可行。

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.

非常感谢。

推荐答案

除了在JavaFX中使用侦听器之外,显然可以在Entity中设置值而不做任何事情。如果你想采用另一种方式,你可以添加 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.

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

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