Is it possible to associate StringProperty with a POJO string in JavaFX?

I am building an application using JavaFX 2. I intend to isolate the user interface from data and logic. Given 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; }
}

I find it difficult to create a binding between the m_Value attribute and the textProperty label, for example.

Another similar question suggested people use JFXtras, however I'm not sure that I am allowed to use this (company restrictions). Therefore, I am trying to find an alternative to this.

My idea would be to use objects to represent all the attributes of my data objects, not primitive types (int will use Integer, etc.). That way, I can take advantage of the java model reference pass. Then, in the user interface domain, I can create a property that references this attribute. But I'm not sure if this is possible or not.

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

Another alternative would be to use the JavaBeanProperty class family, for example, JavaBeanStringPropertyBuilder, JavaBeanIntegerPropertyBuilder. Is it possible?

I tried in both directions, but I am afraid that I am not yet so versed in JavaFX to solve this problem. Can anyone help?

StringProperty IntegerProperty , JavaFX . , Eclipse, , , JavaFX . , .

.

+4
1

, Entity, , JavaFX. , java.beans.PropertyChangeSupport. javafx.

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. string .

+4

Source: https://habr.com/ru/post/1529475/


All Articles