javafx 实现controller间的数据传递
2017-12-14 本文已影响0人
Mixqum
需求描述:
两个fxml界面,当一个界面的输入框TextField填入内容,点击确定后另一个界面Label显示出TextField的内容
假设两个界面的控制器分别是:
ResultController
(显示结果的界面 ResultPage.fxml
)
InputController
(输入字符串的界面Input.fxml
)
- 首先定义一个数据模型
AppModel.java
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class AppModel
{
private StringProperty text = new SimpleStringProperty();
public AppModel()
{
this.text = new SimpleStringProperty();
}
public StringProperty textProperty() {
return text;
}
public final String getText() {
return textProperty().get();
}
public final void setText(String text) {
textProperty().set(text);
}
}
- 然后,在要显示结果的界面控制器
ResultController
定义一个ApplModel实例,在初始化方法中绑定属性监听
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class ResultController implements Initializable
{
// 必须static 类型
public static AppModel model = new AppModel();
@FXML
private Label txtView;
@Override
public void initialize(URL location, ResourceBundle resources) {
// update text area if text in model changes:
model.textProperty().addListener((obs, oldText, newText) -> txtView.setText(newText));
}
public static void setText(String text)
{
model.setText(text);
}
// 打开文本输入窗口
public void open(ActionEvent event) {
// 从模板列表选择一个
try
{
Parent root = FXMLLoader.load(getClass().getResource("Input.fxml"));
Stage stage = new Stage();
stage.setTitle("文本输入窗口");
stage.setScene(new Scene(root));
stage.setFocused(true);
stage.show();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
- 在输入内容的控制器界面
InputController
,调用ResultController的setText方法
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
public class InputController
{
@FXML
private TextField textEnter;
// action event handler for button:
@FXML
private void sendText() {
// 获取结果界面控制器
FXMLLoader loader = new FXMLLoader(getClass().getResource("ResultPage.fxml"));
try
{
AnchorPane login = (AnchorPane) loader.load();
} catch (IOException e)
{
e.printStackTrace();
}
ResultController control = (ResultController) loader.getController();
// 设置结果界面内容
control.model.setText(textEnter.getText());
}
public void confirm(ActionEvent actionEvent)
{
//改变模板设置控制器的模板名列表属性,触发观察者
String inputContent=textEnter.getText();
ResultController.setText(inputContent);
//关闭窗口
((Node) (actionEvent.getSource())).getScene().getWindow().hide();
}
}
附上界面代码:
Input.fxml
:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="180.0" prefWidth="210.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.InputController">
<children>
<TextField fx:id="textEnter" layoutX="14.0" layoutY="42.0" />
<Button layoutX="14.0" layoutY="79.0" mnemonicParsing="false" onAction="#confirm" text="确定" />
</children>
</AnchorPane>
ResultPage.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="297.0" prefWidth="285.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ResultController">
<children>
<Label fx:id="txtView" layoutX="48.0" layoutY="46.0" prefHeight="34.0" prefWidth="135.0" text="result:" />
<Button layoutX="48.0" layoutY="95.0" mnemonicParsing="false" onAction="#open" text="打开文本输入窗口" />
</children>
</AnchorPane>
程序入口类Main.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application
{
@Override
public void start(Stage primaryStage) {
Parent root;
try
{
root = FXMLLoader.load(getClass().getResource("ResultPage.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("控制器间数据传递");
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}