在JavaFX中连接视图和模型的主要方法是什么? [英] What is the main way to connect a view and a model in JavaFX?

查看:178
本文介绍了在JavaFX中连接视图和模型的主要方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaFX中连接视图和模型的预期方法是什么?

What is an expected method of connecting view and model in JavaFX?

绑定?

假设我想要使用以下控件在数据库中进行定位:

Suppose I want to make positioning in database with the following controls:

我在内存中有数据(记录集)对象,它的属性是可绑定的。即他们正在通知当前记录何时更改以及记录数量何时更改。

I have data (recordset) object in memory and it's properties are bindable. I.e. they are notifying when current record changes and when the number of records changes.

我希望用户能够使用滑块和文本字段在记录集内定位。

I want user to be able position inside recordset both with slider and text field.

如何实现? JavaFX中没有数字旋转,那么如何将文本,滑块和记录集对象(三端)绑定在一起?有可能吗?

How to accomplish that? There is no numeric spin in JavaFX, so how to bind text, slider and recordset object (three ends) together? Is it possible?

推荐答案

我不能给出权威的答案,因为我不为Oracle工作,也不是JavaFX-pert但我通常使用模型实例构造控制器,并在控制器的初始化方法中创建控件属性和模型属性之间的绑定。

I cannot give an authoritative answer since I don't work for Oracle nor am I some JavaFX-pert but I usually construct the controller with an instance of the model and in the initialize method of the controller I create the bindings between the controls' properties and the model's properties.

To你的具体问题我有一个蹩脚但有效的解决方案。 Lame因为我无法弄清楚如何使用低级绑定API而我在模型上使用了两个属性。方法如下:

To your concrete problem I have a lame but working solution. Lame because I can't figure out how to use the low-level binding API and I use two properties on the model. Here's how :

FXML:

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>

<?import javafx.scene.control.TextField?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">

    <Slider GridPane.columnIndex="0" fx:id="slider" min="0" max="99"/>
    <Label GridPane.columnIndex="1" text="Record" />
    <TextField fx:id="textfield" GridPane.columnIndex="2"/>
</GridPane>

Main.java:

Main.java :

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import sample.Models.Model;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        final Model model = new Model();

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("sample.fxml"));
        loader.setControllerFactory(new Callback<Class<?>, Object>() {
            @Override
            public Object call(Class<?> aClass) {
                return new Controller(model);
            }
        });
        GridPane root = (GridPane) loader.load();

        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }
}

Controller.java:

Controller.java :

package sample;

import javafx.beans.binding.DoubleBinding;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import sample.Models.Model;

import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    private final Model model;

    public Controller(Model model) {
        this.model = model;
    }

    @FXML
    public Slider slider;
    @FXML
    public TextField textfield;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        slider.valueProperty().bindBidirectional(model.pageProperty());
        textfield.textProperty().bindBidirectional(model.pageTextProperty());
    }
}

Model.java:

Model.java :

package sample.Models;

package sample.Models;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

public class Model {

    private IntegerProperty pageProperty;
    private StringProperty pageTextProperty;

    public Model() {
        pageProperty = new SimpleIntegerProperty(2);
        pageProperty.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
                int value = pageProperty.get();
                //System.out.println("Page changed to " + value);
                if (Integer.toString(value).equals(pageTextProperty.get())) return;
                pageTextProperty.set(Integer.toString(value));
            }
        });

        pageTextProperty = new SimpleStringProperty("2");
        pageTextProperty.addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String s, String s2) {
                try {
                    int parsedValue = Integer.parseInt(observableValue.getValue());
                    if (parsedValue == pageProperty.get()) return;
                    pageProperty().set(parsedValue);
                } catch (NumberFormatException e) {
                    // too bad
                }
            }
        });
    }

    public int getPage() {
        return pageProperty.get();
    }

    public void setPage(int page) {
        pageProperty.set(page);
    }

    public IntegerProperty pageProperty() {
        return pageProperty;
    }

    public String getPageText() {
        return pageTextProperty.get();
    }

    public void setPageText(String pageText) {
        pageTextProperty.set(pageText);
    }

    public StringProperty pageTextProperty() {
        return pageTextProperty;
    }
}

这篇关于在JavaFX中连接视图和模型的主要方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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