我有两个javaFx窗口。第一个有T/N
和ComboBox
打开第二个模态窗口。
在第二个窗口中有Button
和TextField
Button
,可将textField值添加到Control
窗口MainController
。我不知道该怎么做。有一个解释的例子对我很有帮助。以下是课程:
Main.java
ComboBox
Contrller.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
主窗口的FXML:
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable{
@FXML
public ComboBox<String> combo;
@FXML
Button button;
public ObservableList<String> list = FXCollections.observableArrayList("A");
@Override
public void initialize(URL location, ResourceBundle resources) {
setCombo();
}
public void setCombo(){
combo.setItems(list);
}
public void openModal() throws IOException {
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("sec.fxml"));
primaryStage.setTitle("Send Mail");
primaryStage.setScene(new Scene(root,800,600));
primaryStage.initModality(Modality.WINDOW_MODAL);
//primaryStage.initOwner((Stage) menuBar.getScene().getWindow());
primaryStage.show();
}
}
现在第二个窗口Sec.Java
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<StackPane prefHeight="67.0" prefWidth="600.0">
<children>
<Label text="Get value from Another Child Dialog" />
</children>
</StackPane>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<Label text="Add Value of TextBox:" />
<ComboBox fx:id="combo" prefWidth="150.0" />
</children>
</HBox>
<Button fx:id="button" mnemonicParsing="false" onAction="#openModal" text="Open Dialog" />
</children>
</VBox>
第二个窗口FXML:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
/**
* Created by tracedot on 12/11/16.
*/
public class Sec implements Initializable{
@FXML
Button button;
@FXML
TextField textfield;
public Controller controller=new Controller();
public void setToCombo(){
String cbvalue= textfield.getText();
controller.combo.getItems().add(cbvalue);
//controller.combo.itemsProperty().setValue(new ArrayList<String>().add());
}
@Override
public void initialize(URL location, ResourceBundle resources) {
//setToCombo();
}
}
答案 0 :(得分:0)
创建一个也用于主fxml的Controller
类的新实例,可以帮助您与显示的fxml一起使用的实例进行通信。您必须将现有控制器传递给与第二个fxml使用的Sec
实例:
public class Sec implements Initializable{
...
private Controller controller;
public void setController(Controller controller ) {
this.controller = controller;
}
...
}
public void openModal() throws IOException {
Stage primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("sec.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Send Mail");
primaryStage.setScene(new Scene(root,800,600));
primaryStage.initModality(Modality.WINDOW_MODAL);
//primaryStage.initOwner((Stage) menuBar.getScene().getWindow());
loader.<Sec>getController().setController(this);
primaryStage.show();
}
如果您想在输入后关闭新窗口,还可以使用showAndWait()
显示新阶段:
public class Sec implements Initializable{
...
private String result = null;
public String getResult() {
return result;
}
public void setCombo() {
result = textfield.getText();
textField.getScene().getWindow().hide();
}
...
}
public void openModal() throws IOException {
Stage primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("sec.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Send Mail");
primaryStage.setScene(new Scene(root,800,600));
primaryStage.initModality(Modality.WINDOW_MODAL);
//primaryStage.initOwner((Stage) menuBar.getScene().getWindow());
primaryStage.showAndWait();
String result = loader.<Sec>getController().getResult();
if (result != null) {
// if a result was selected, add it to the list
list.add(result);
}
}
请注意,使用此方法,如果要重复使用第二个场景,还应添加一些功能以重置result
字段。