将TableView数据显示到另一个Window中,包含JavaFx中的TextField [英] show TableView data into another Window contains TextField in JavaFx
问题描述
我制作了两个包含TextField的Fxml文件,另一个包含TableView。它有它的Controller类。我想在执行鼠标单击操作事件时将TableView中的数据显示到TextField。但我们没有得到结果它显示了很多错误,如:
2018年6月2日上午8:33:36 javafx.fxml.FXMLLoader $ ValueElement processValue
警告:JavaFX运行时版本8.0.162加载版本9.0.1的JavaFX API的FXML文档
2018年6月2日上午8:33:44 com.sun .javafx.css.StyleManager loadStylesheetUnPrivileged
INFO:找不到样式表:file:/ C:/Users/MdAzaz/IdeaProjects/JavaFxProject3/out/production/Stylesheet/style.css
Jun 02,2018 8 :33:46 AM javafx.fxml.FXMLLoader $ ValueElement processValue
警告:JavaFX运行时版本8.0.162加载版本9.0.1的JavaFX API的FXML文档
2018年6月2日8:33: 50 AM javafx.fxml.FXMLLoader $ ValueElement processValue
警告:JavaFX运行时版本8.0.162加载版本9.0.1的JavaFX API的FXML文档
java.lang.NullPointerException
at Company .CompanyTableController.Clicked(CompanyTableController.java:123)
at Company.CompanyTabl eController.access $ 100(CompanyTableController.java:35)
at Company.CompanyTableController $ 2.handle(CompanyTableController.java:108)
at Company.CompanyTableController $ 2.handle(CompanyTableController.java:105)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com。 sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx。 event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher。 dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispat chChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent( EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java: 114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx .event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene $ ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene $ ClickGenerator.access $ 8100( Scene.java:3398)
at javafx.scene.Scene $ MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene $ MouseHandler.access $ 1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx。 tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:295)
at java。 security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk。 quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui。 View.handleMouseEvent(查看。 java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda $ null $ 147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
流程已完成退出代码0
第一个Fxml文件名CompanyLayout.fxml包含TextFields和它的Controller类与CompanyController.java一样。它有按钮名称查找,当我点击该按钮时,打开包含TableView的CompanyTable.fxml。当我点击TableView的特定行时,该行的数据将显示在包含Textfields的CompanyLayout.fxml上。
查找CompanyController.java的按钮代码是这样的:
btnFind .setOnAction(new EventHandler< ActionEvent>(){
@Override
public void handle(ActionEvent event){
try {
Stage primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader();
父root = loader.load(getClass()。getClassLoader()。getResource(Company \\CompanyTable.fxml));
场景场景= new Scene(root);
scene.getStylesheets()。add(getClass()。getClassLoader()。getResource(Stylesheet \\style.css)。toExternalForm());
primaryStage .setTitle(Company Table);
primaryStage.setScene(scene);
primaryStage.show();
} catch(例外e){
e .printStackTrace();
}
}
});
CompanyTableController.java的TableView代码是这样的:
CompanyTable.setOnMouseClicked(new EventHandler< MouseEvent>(){
@Override
public void handle(MouseEvent event){
Clicked();
}
});
private void Clicked()
{
try
{
CompanyData categoryData = CompanyTable.getItems()。get(CompanyTable.getSelectionModel()。getSelectedIndex ());
CategoryID.setText(categoryData.getCompanyID());
CategoryName.setText(categoryData.getCompanyName());
}
catch(例外e)
{
e.printStackTrace();
}
}
我希望你能理解这段代码,请帮忙我!
我创建了一个演示你的问题的示例应用程序。
关键是将数据从一个控制器
传递给另一个。您可以在此处找到相关信息。
以下是关键代码:
@FXML
private void handleButtonAction(ActionEvent event){
try
{
FXMLLoader loader = new FXMLLoader(getClass()。getResource(SearchPanel.fxml));
父root = loader.load();
SearchPanelController searchPanelController = loader.getController(); //访问Controller
阶段阶段=新阶段();
stage.setScene(new Scene(root));
stage.showAndWait(); //等待搜索控制器关闭。
客户tempCustomer = searchPanelController.getCustomer(); //从搜索控制器获取所选客户。看看SearchPanelController吧!
//将所选客户设置为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
* /
公共类JavaFXApplication3扩展应用程序{
@Override
public void start(阶段阶段)抛出异常{
父root = FXMLLoader.load(getClass()。getResource(FXMLDocument.fxml));
场景场景=新场景(根);
stage.setScene(场景);
stage.show();
}
/ **
* @param args命令行参数
* /
public static void main(String [] args){
launch(args);
}
}
客户类别:
/ **
*
* @author sedrick
* /
公共类客户{
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 firstName
* /
public String getFirstName(){
return名字;
}
/ **
* @param firstName firstName设置
* /
public void setFirstName(String firstName){
this.firstName = firstName;
}
/ **
* @return lastName
* /
public String getLastName(){
return lastName;
}
/ **
* @param lastName设置的lastName为
* /
public void setLastName(String lastName){
this.lastName = lastName;
}
/ **
* @return电子邮件
* /
public String getEmail(){
return email;
}
/ **
* @param通过电子邮件发送电子邮件至
* /
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
* /
公共类FXMLDocumentController实现Initializable {
@FXML TextField tfFirstName,tfLastName,tfEmail;
@FXML
private void handleButtonAction(ActionEvent event){
try
{
FXMLLoader loader = new FXMLLoader(getClass()。getResource(SearchPanel .fxml));
父root = loader.load();
SearchPanelController searchPanelController = loader.getController();
舞台舞台=新舞台();
stage.setScene(new Scene(root));
stage.showAndWait();
客户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
}
}
FXMLDocument FXML:
<?xml version =1.0encoding =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 = - InfinitymaxWidth = - InfinityminHeight = - InfinityminWidth = - InfinityprefHeight =484.0prefWidth =667.0xmlns =http: //javafx.com/javafx/8.0.141xmlns:fx =http://javafx.com/fxml/1fx:controller =javafxapplication3.FXMLDocumentController>
< children>
< MenuBar>
< menus>
< Menu mnemonicParsing =falsetext =File>
< items>
< MenuItem mnemonicParsing =falsetext =Close/>
< / items>
< /菜单>
< Menu mnemonicParsing =falsetext =Edit>
< items>
< MenuItem mnemonicParsing =falsetext =Delete/>
< / items>
< /菜单>
< Menu mnemonicParsing =falsetext =Help>
< items>
< MenuItem mnemonicParsing =falsetext =About/>
< / items>
< /菜单>
< / menus>
< / MenuBar>
< StackPane prefHeight =150.0prefWidth =200.0VBox.vgrow =ALWAYS>
< children>
< VBox alignment =CENTERmaxHeight = - InfinitymaxWidth = - Infinity>
< children>
< HBox maxHeight = - InfinitymaxWidth = - Infinity>
< children>
< Label maxHeight =1.7976931348623157E308text =First Name:/>
< TextField fx:id =tfFirstName/>
< / children>
< / HBox>
< HBox maxHeight = - InfinitymaxWidth = - Infinity>
< children>
< Label maxHeight =1.7976931348623157E308maxWidth = - InfinityprefWidth =60.0text =姓氏:/>
< TextField fx:id =tfLastName/>
< / children>
< / HBox>
< HBox maxHeight = - InfinitymaxWidth = - Infinity>
< children>
< Label alignment =CENTER_RIGHTmaxHeight =1.7976931348623157E308prefWidth =60.0text =Email:/>
< TextField fx:id =tfEmail/>
< / children>
< / HBox>
< Button alignment =CENTERmnemonicParsing =falseonAction =#handleButtonActiontext =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控制器类
*
* @author sedri
* /
公共类SearchPanelController实现Initializable {
@FXML TableView< Customer> tvSearch;
@FXML TableColumn tcFirstName,tcLastName,tcEmail;
客户选择客户;
/ **
*初始化控制器类。
* @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(
新客户(Jacob,Smith,jacob.smith@example.com),
新客户(Isabella,Johnson ,isabella.johnson@example.com),
新客户(Ethan,Williams,ethan.williams@example.com),
新客户(Emma, Jones,emma.jones @ example.com),
新客户(Michael,Brown,michael.brown@example.com)
);
tvSearch.setItems(data);
}
public Customer getCustomer()
{
return selectedCustomer;
}
}
SearchPanel FXML:
<?xml version =1.0encoding =UTF-8?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.StackPane?>
< StackPane maxHeight = - InfinitymaxWidth = - InfinityminHeight = - InfinityminWidth = - InfinityprefHeight =400.0prefWidth =600.0xmlns =http: //javafx.com/javafx/8.0.141xmlns:fx =http://javafx.com/fxml/1fx:controller =javafxapplication3.SearchPanelController>
< children>
< TableView fx:id =tvSearchprefHeight =200.0prefWidth =200.0>
< columns>
< TableColumn fx:id =tcFirstNameprefWidth =153.0text =First Name/>
< TableColumn fx:id =tcLastNameprefWidth =182.0text =姓氏/>
< TableColumn fx:id =tcEmailprefWidth =263.0text =Email/>
< / columns>
< / TableView>
< / children>
< / StackPane>
I have made Two Fxml file one contains TextField and another contains TableView. And it has its Controller class. I want to show data from TableView to TextField when mouse clicked action event is performed. But we didn't get the result it shows a lots of error like :
Jun 02, 2018 8:33:36 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.162
Jun 02, 2018 8:33:44 AM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not find stylesheet: file:/C:/Users/MdAzaz/IdeaProjects/JavaFxProject3/out/production/Stylesheet/style.css
Jun 02, 2018 8:33:46 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.162
Jun 02, 2018 8:33:50 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.162
java.lang.NullPointerException
at Company.CompanyTableController.Clicked(CompanyTableController.java:123)
at Company.CompanyTableController.access$100(CompanyTableController.java:35)
at Company.CompanyTableController$2.handle(CompanyTableController.java:108)
at Company.CompanyTableController$2.handle(CompanyTableController.java:105)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Process finished with exit code 0
The first Fxml file name CompanyLayout.fxml contains TextFields and it has its Controller class like CompanyController.java. It has Button name Find when i clicked on that button CompanyTable.fxml is opened which contain TableView. When i clicked on perticular row of TableView the data of that row will display on CompanyLayout.fxml which contains Textfields.
Find Button code for CompanyController.java is this:
btnFind.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
Stage primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(getClass().getClassLoader().getResource("Company\\CompanyTable.fxml"));
Scene scene=new Scene(root);
scene.getStylesheets().add(getClass().getClassLoader().getResource("Stylesheet\\style.css").toExternalForm());
primaryStage.setTitle("Company Table");
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
TableView Code for CompanyTableController.java is this:
CompanyTable.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Clicked();
}
});
private void Clicked()
{
try
{
CompanyData categoryData=CompanyTable.getItems().get(CompanyTable.getSelectionModel().getSelectedIndex());
CategoryID.setText(categoryData.getCompanyID());
CategoryName.setText(categoryData.getCompanyName());
}
catch(Exception e)
{
e.printStackTrace();
}
}
I hope you will understand this code so, please help me!
I created a sample app that demos your question.
The key is passing data from one Controller
to another. You can find info here.
Here is the key code:
@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();
}
}
Full App - Main Class:
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);
}
}
Customer Class:
/**
*
* @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 Class:
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
}
}
FXMLDocument FXML:
<?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 Class:
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;
}
}
SearchPanel 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>
这篇关于将TableView数据显示到另一个Window中,包含JavaFx中的TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!