我制作了两个包含TextField的Fxml文件,另一个包含TableView。它有它的Controller类。我想在执行鼠标单击操作事件时将TableView中的数据显示到TextField。但我们没有得到结果它显示了很多错误,如:
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
第一个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();
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();
}
}
});
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(Exception e)
{
e.printStackTrace();
}
}
我希望你能理解这段代码,请帮助我!
答案 0 :(得分:0)
我创建了一个演示您的问题的示例应用。
关键是将数据从一个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();
}
}
。您可以找到信息here。
以下是关键代码:
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;
}
}
客户类:
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
}
}
FXMLDocumentController类:
<?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>
FXMLDocument FXML:
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;
}
}
SearchPanelController类:
<?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>
SearchPanel FXML:
var parts ='01/01/1983'.split('/');
var mydate = new Date(parseInt(parts[2]) + 1, parts[1] - 1, parts[0]);
console.log(mydate.toDateString())