我试图制作一个简单的程序来存储客户详细信息,但是当我单击编辑按钮而不是 EditCustomerDialog.fxml 显示时,我得到了一个我无法弄清楚的运行时异常。
Eclipse Mars.1 并使用 JavaFX Scene Builder 8.0.0 构建了 fxml
使用 Java 8 编译
提前谢谢。
主.java
package com.ed.gct;
import java.io.IOException;
import java.util.ResourceBundle;
import com.ed.gct.model.Customer;
import com.ed.gct.view.*;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private ObservableList<Customer> customerData = FXCollections.observableArrayList();
public Main(){
customerData.add(new Customer("Andrew", "Eastwood"));
customerData.add(new Customer("Samantha", "Eastwood"));
customerData.add(new Customer("Joshua", "Eastwood"));
customerData.add(new Customer("Lacey", "Eastwood"));
customerData.add(new Customer("John", "Smith"));
customerData.add(new Customer("Some", "Guy"));
}
public ObservableList<Customer> getCustomerData() {
return customerData;
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Gold Coast Tanning");
initRootLayout();
showCustomerOverview();
}
private void initRootLayout() {
// TODO Auto-generated method stub
try{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
}catch (IOException e){
e.printStackTrace();
}
}
public void showCustomerOverview() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/CustomerOverview.fxml"));
AnchorPane customerOverview = (AnchorPane) loader.load();
rootLayout.setCenter(customerOverview);
CustomerOverviewController controller = loader.getController();
controller.setMain(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
public boolean showCustomerEditDialog(Customer customer) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CustomerEditDialog.fxml"));
AnchorPane page = (AnchorPane)fxmlLoader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Edit Customer");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
CustomerEditController controller = fxmlLoader.getController();
controller.setDialogStage(dialogStage);
controller.setCustomer(customer);
dialogStage.showAndWait();
return controller.isOkClicked();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
CustomerEditController.java
package com.ed.gct.view;
import com.ed.gct.model.Customer;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import javafx.util.converter.IntegerStringConverter;
public class CustomerEditController {
@FXML
public Label firstNameEdit;
@FXML
public Label lastNameEdit;
@FXML
public Label addressEdit;
@FXML
public Label townEdit;
@FXML
public Label postcodeEdit;
@FXML
public Label dobEdit;
@FXML
public Label skinTypeEdit;
@FXML
public Label minsEdit;
@FXML
public TextArea medicalEdit;
private SimpleStringProperty firstName;
private StringProperty lastName;
private StringProperty address;
private StringProperty town;
private StringProperty postcode;
private StringProperty skinType;
private StringProperty dob;
private IntegerProperty mins;
private StringProperty medical;
private Stage dialogStage;
private Customer customer;
private boolean okClicked = false;
private IntegerStringConverter convert;
@FXML
private void initialize(){
convert = new IntegerStringConverter();
}
public void setDialogStage(Stage dialogStage){
this.dialogStage = dialogStage;
}
public void setCustomer(Customer customer){
this.customer = customer;
firstNameEdit.setText(customer.getFirstNameProperty().get());
lastNameEdit.setText(customer.getLastNameProperty().get());
addressEdit.setText(customer.getAddressProperty().get());
townEdit.setText(customer.getTownProperty().get());
postcodeEdit.setText(customer.getPostcodeProperty().get());
dobEdit.setText(customer.getDobProperty().get());
skinTypeEdit.setText(customer.getSkinTypeProperty().get());
minsEdit.setText(convert.toString(customer.getMinsProperty().get()));
medicalEdit.setText(customer.getMedicalProperty().get());
}
public boolean isOkClicked(){
return okClicked;
}
@FXML
private void handleOk() {
if (isInputValid()) {
firstName = new SimpleStringProperty(firstNameEdit.getText());
lastName = new SimpleStringProperty(lastNameEdit.getText());
address = new SimpleStringProperty(addressEdit.getText());
town = new SimpleStringProperty(townEdit.getText());
postcode = new SimpleStringProperty(postcodeEdit.getText());
dob = new SimpleStringProperty(dobEdit.getText());
skinType = new SimpleStringProperty(skinTypeEdit.getText());
mins = new SimpleIntegerProperty(convert.fromString(minsEdit.getText()));
medical = new SimpleStringProperty(medicalEdit.getText());
customer.setFirstNameProperty(firstName);
customer.setLastNameProperty(lastName);
customer.setAddressProperty(address);
customer.setTownProperty(town);
customer.setPostcodeProperty(postcode);
customer.setDobProperty(dob);
customer.setSkinTypeProperty(skinType);
customer.setMinsProperty(mins);
customer.setMedicalproperty(medical);
okClicked = true;
dialogStage.close();
}
}
@FXML
private void handleCancel() {
dialogStage.close();
}
private boolean isInputValid() {
String errorMessage = "";
if (firstNameEdit.getText() == null || firstNameEdit.getText().length() == 0) {
errorMessage += "No valid first name!\n";
}
if (lastNameEdit.getText() == null || lastNameEdit.getText().length() == 0) {
errorMessage += "No valid last name!\n";
}
if (townEdit.getText() == null || townEdit.getText().length() == 0) {
errorMessage += "No valid town !\n";
}
if (postcodeEdit.getText() == null || postcodeEdit.getText().length() == 0) {
errorMessage += "No valid postal code!\n";
}
if (dobEdit.getText() == null || dobEdit.getText().length() == 0) {
errorMessage += "No valid date of birth!\n";
}
if (skinTypeEdit.getText() == null || skinTypeEdit.getText().length() == 0) {
errorMessage += "No valid skin type!\n";
}
if (errorMessage.length() == 0) {
return true;
} else {
// Show the error message.
Alert alert = new Alert(AlertType.ERROR);
alert.initOwner(dialogStage);
alert.setTitle("Invalid Fields");
alert.setHeaderText("Please correct invalid fields");
alert.setContentText(errorMessage);
alert.showAndWait();
return false;
}
}
}
CustomerOverviewController.java
package com.ed.gct.view;
import java.util.Optional;
import com.ed.gct.Main;
import com.ed.gct.model.Customer;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.util.converter.IntegerStringConverter;
public class CustomerOverviewController {
@FXML
private TableView<Customer> customerTable;
@FXML
private TableColumn<Customer, String> firstNameColumn;
@FXML
private TableColumn<Customer, String> lastNameColumn;
@FXML
private Label firstNameLabel;
@FXML
private Label lastNameLabel;
@FXML
private Label addressLabel;
@FXML
private Label townLabel;
@FXML
private Label postcodeLabel;
@FXML
private Label dobLabel;
@FXML
private Label skinTypeLabel;
@FXML
private Label minsLabel;
@FXML
private TextArea medicalArea;
String name;
String lName;
private Main main;
private IntegerStringConverter convert;
public CustomerOverviewController(){
convert = new IntegerStringConverter();
}
@FXML
private void initialize(){
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().getFirstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().getLastNameProperty());
showCustomerDetails(null);
customerTable.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showCustomerDetails(newValue));
}
public void setMain(Main main) {
this.main = main;
customerTable.setItems(main.getCustomerData());
}
public void showCustomerDetails(Customer customer){
if (customer != null){
firstNameLabel.setText(customer.getFirstNameProperty().get());
lastNameLabel.setText(customer.getLastNameProperty().get());
addressLabel.setText(customer.getAddressProperty().get());
townLabel.setText(customer.getTownProperty().get());
postcodeLabel.setText(customer.getPostcodeProperty().get());
skinTypeLabel.setText(customer.getSkinTypeProperty().get());
dobLabel.setText(customer.getDobProperty().get());
int mins = customer.getMinsProperty().get();
String minsVal = convert.toString(mins);
minsLabel.setText(minsVal);
medicalArea.setText(customer.getMedicalProperty().get());
name = customer.getFirstNameProperty().get();
lName = customer.getLastNameProperty().get();
}else{
firstNameLabel.setText("");
lastNameLabel.setText("");
addressLabel.setText("");
townLabel.setText("");
postcodeLabel.setText("");
skinTypeLabel.setText("");
dobLabel.setText("");
minsLabel.setText("");
medicalArea.setText("No previous history");
}
}
@FXML
private void handleDeleteCustomer(){
int selectedIndex = customerTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
Alert confirm = new Alert(Alert.AlertType.CONFIRMATION);
confirm.setTitle("Delete Customer...");
confirm.setContentText("Are you sure you wish to delete "+ name + " " + lName);
Optional<ButtonType> result = confirm.showAndWait();
if ((result.isPresent()) && (result.get() == ButtonType.OK)){
customerTable.getItems().remove(selectedIndex);
}
} else {
Alert alert = new Alert(AlertType.WARNING);
alert.initOwner(main.getPrimaryStage());
alert.setTitle("No Selection");
alert.setHeaderText("No Customer Selected");
alert.setContentText("Please select a Customer in the table.");
alert.showAndWait();
}
}
@FXML
private void handleNewCustomer() {
Customer tempCustomer = new Customer();
boolean okClicked = main.showCustomerEditDialog(tempCustomer);
if (okClicked) {
main.getCustomerData().add(tempCustomer);
}
}
@FXML
private void handleEditCustomer() {
Customer selectedCustomer = customerTable.getSelectionModel().getSelectedItem();
if (selectedCustomer != null) {
boolean okClicked = main.showCustomerEditDialog(selectedCustomer);
if (okClicked) {
showCustomerDetails(selectedCustomer);
}
} else {
Alert alert = new Alert(AlertType.WARNING);
alert.initOwner(main.getPrimaryStage());
alert.setTitle("No Selection");
alert.setHeaderText("No Customer Selected");
alert.setContentText("Please select a customer in the table.");
alert.showAndWait();
}
}
}
CustomerEditDialog.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ed.gct.view.CustomerEditController">
<children>
<GridPane layoutX="97.0" layoutY="176.0" AnchorPane.bottomAnchor="70.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="241.0" minWidth="10.0" prefWidth="120.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="380.0" minWidth="10.0" prefWidth="380.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="37.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="73.0" minHeight="10.0" prefHeight="37.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="90.0" minHeight="10.0" prefHeight="37.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="98.0" minHeight="10.0" prefHeight="37.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="147.0" minHeight="10.0" prefHeight="37.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="37.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="139.0" minHeight="10.0" prefHeight="37.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="139.0" minHeight="10.0" prefHeight="37.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="150.0" valignment="CENTER" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="First Name">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Last Name" GridPane.rowIndex="1">
<font>
<Font size="14.0" />
</font>
</Label>
<Label GridPane.rowIndex="2" />
<Label text="D.O.B" GridPane.rowIndex="2">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Address" GridPane.rowIndex="3">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Town" GridPane.rowIndex="4">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Post Code" GridPane.rowIndex="5">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Skin Type" GridPane.rowIndex="6">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Mins" GridPane.rowIndex="7">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Medical" GridPane.rowIndex="8">
<font>
<Font size="14.0" />
</font>
</Label>
<TextField fx:id="firstNameEdit" GridPane.columnIndex="1" />
<TextField fx:id="lastNameEdit" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="dobEdit" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField fx:id="addressEdit" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<TextField fx:id="townEdit" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<TextField fx:id="postcodeEdit" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<TextField fx:id="skinTypeEdit" GridPane.columnIndex="1" GridPane.rowIndex="6" />
<TextField fx:id="minsEdit" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<TextArea fx:id="medicalEdit" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="8" />
</children>
</GridPane>
<ButtonBar layoutX="286.0" layoutY="446.0" prefHeight="40.0" prefWidth="200.0">
<buttons>
<Button mnemonicParsing="false" onAction="#handleOk" text="OK" />
<Button mnemonicParsing="false" onAction="#handleCancel" text="Cancel" />
</buttons>
</ButtonBar>
</children>
</AnchorPane>
新的堆栈跟踪
javafx.fxml.LoadException:
/C:/Users/Sammy%20Poo%20xx/workspace/Gold%20Coast%202/Gold%20Coast%20Tannng/bin/com/ed/gct/view/CustomerEditDialog.fxml:74
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at com.ed.gct.Main.showCustomerEditDialog(Main.java:95)
at com.ed.gct.view.CustomerOverviewController.handleEditCustomer(CustomerOverviewController.java:138)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.Label field com.ed.gct.view.CustomerEditController.firstNameEdit to javafx.scene.control.TextField
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
at javafx.fxml.FXMLLoader.injectFields(Unknown Source)
at javafx.fxml.FXMLLoader.access$1600(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processValue(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
... 71 more