Я создал пример приложения, которое демонстрирует ваш вопрос.
Ключ передает данные из одного Controller
в другой.Вы можете найти информацию здесь .
Вот код ключа:
@FXML
private void handleButtonAction(ActionEvent event) {
try
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("SearchPanel.fxml"));
Parent root = loader.load();
SearchPanelController searchPanelController = loader.getController();//Get access to the Controller
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.showAndWait();//Wait for the Search Controller to close.
Customer tempCustomer = searchPanelController.getCustomer();//Get the selected customer from the Search Controller. HAVE A LOOK AT THE SearchPanelController!
//Set the selected customer to the TextFields
tfFirstName.setText(tempCustomer.getFirstName());
tfLastName.setText(tempCustomer.getLastName());
tfEmail.setText(tempCustomer.getEmail());
} catch (IOException e)
{
e.printStackTrace();
}
}
Полное приложение - Основной класс:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author sedri
*/
public class JavaFXApplication3 extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Класс клиента:
/**
*
* @author sedrick
*/
public class Customer {
private String firstName;
private String lastName;
private String email;
public Customer(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
}
FXMLDocumentController Класс:
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.TextField;
import javafx.stage.Stage;
/**
*
* @author sedri
*/
public class FXMLDocumentController implements Initializable {
@FXML TextField tfFirstName, tfLastName, tfEmail;
@FXML
private void handleButtonAction(ActionEvent event) {
try
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("SearchPanel.fxml"));
Parent root = loader.load();
SearchPanelController searchPanelController = loader.getController();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.showAndWait();
Customer tempCustomer = searchPanelController.getCustomer();
tfFirstName.setText(tempCustomer.getFirstName());
tfLastName.setText(tempCustomer.getLastName());
tfEmail.setText(tempCustomer.getEmail());
} catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
FXMLDocumentFXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="484.0" prefWidth="667.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication3.FXMLDocumentController">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<StackPane prefHeight="150.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<children>
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity">
<children>
<HBox maxHeight="-Infinity" maxWidth="-Infinity">
<children>
<Label maxHeight="1.7976931348623157E308" text="First Name:" />
<TextField fx:id="tfFirstName" />
</children>
</HBox>
<HBox maxHeight="-Infinity" maxWidth="-Infinity">
<children>
<Label maxHeight="1.7976931348623157E308" maxWidth="-Infinity" prefWidth="60.0" text="Last Name:" />
<TextField fx:id="tfLastName" />
</children>
</HBox>
<HBox maxHeight="-Infinity" maxWidth="-Infinity">
<children>
<Label alignment="CENTER_RIGHT" maxHeight="1.7976931348623157E308" prefWidth="60.0" text="Email: " />
<TextField fx:id="tfEmail" />
</children>
</HBox>
<Button alignment="CENTER" mnemonicParsing="false" onAction="#handleButtonAction" text="Search" />
</children>
</VBox>
</children>
</StackPane>
</children>
</VBox>
Класс SearchPanelController:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
/**
* FXML Controller class
*
* @author sedri
*/
public class SearchPanelController implements Initializable {
@FXML TableView<Customer> tvSearch;
@FXML TableColumn tcFirstName, tcLastName, tcEmail;
Customer selectedCustomer;
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
tvSearch.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection)->{
if(newSelection != null)
{
selectedCustomer = newSelection;
tvSearch.getScene().getWindow().hide();
}
});
tcFirstName.setCellValueFactory(new PropertyValueFactory("firstName"));
tcLastName.setCellValueFactory(new PropertyValueFactory("lastName"));
tcEmail.setCellValueFactory(new PropertyValueFactory("email"));
ObservableList<Customer> data =
FXCollections.observableArrayList(
new Customer("Jacob", "Smith", "jacob.smith@example.com"),
new Customer("Isabella", "Johnson", "isabella.johnson@example.com"),
new Customer("Ethan", "Williams", "ethan.williams@example.com"),
new Customer("Emma", "Jones", "emma.jones@example.com"),
new Customer("Michael", "Brown", "michael.brown@example.com")
);
tvSearch.setItems(data);
}
public Customer getCustomer()
{
return selectedCustomer;
}
}
Панель поиска FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.StackPane?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication3.SearchPanelController">
<children>
<TableView fx:id="tvSearch" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="tcFirstName" prefWidth="153.0" text="First Name" />
<TableColumn fx:id="tcLastName" prefWidth="182.0" text="Last Name" />
<TableColumn fx:id="tcEmail" prefWidth="263.0" text="Email" />
</columns>
</TableView>
</children>
</StackPane>