首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法从DialogPane,JavaFX的TextField输入中读取文本

无法从DialogPane,JavaFX的TextField输入中读取文本
EN

Stack Overflow用户
提问于 2019-03-12 01:34:24
回答 2查看 383关注 0票数 0

我正在尝试在JavaFX中创建一个简单的联系人应用程序。当我想创建新联系人时,它有一个带有TableView的主窗口和一个打开的DialogPane。在DialogPane中有几个TextFields,我想要从中收集文本,以便创建联系人列表。我的问题是,当我想要从DialogPane (来自TextFields)读取输入时,应用程序会运行一个错误(java.lang.NullPointerException)。当我在我的主窗口FXML文件中放入一个TextField时,我就可以很好地从textField访问这个文本了。当我想从DialogPane读取数据时,为什么我得到一个错误(错误-->文件: Controller.java,我注释了我得到一个错误的部分)?我被卡住了。有没有人能建议我哪里做错了?下面是我的代码:

Main.java

代码语言:javascript
复制
public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("mainwindow.fxml"));
    primaryStage.setTitle("Your Contacts");
    primaryStage.setScene(new Scene(root, 900, 600));
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}
}

Controller.java

代码语言:javascript
复制
public class Controller {


@FXML
private BorderPane mainPanel;


public ObservableList<Contact> contactsList = FXCollections.observableArrayList();


@FXML
public void showAddContactDialog() {
    Dialog<ButtonType> dialog = new Dialog<>();
    dialog.initOwner(mainPanel.getScene().getWindow());
    dialog.setTitle("Add new contact");
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("addContactDialog.fxml"));
    try {
       dialog.getDialogPane().setContent(fxmlLoader.load());
    } catch (IOException e) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText(null);
        alert.setContentText("Couldn't load the dialog");
        alert.showAndWait();
        e.printStackTrace();
        return;
    }

    dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
    dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);

    Optional<ButtonType> result = dialog.showAndWait();
    if(result.isPresent() && result.get() == ButtonType.OK) {

    //========================================================================================

        //here is the error, I cannot read values from addContactDialog controller

        ContactController contactController = new ContactController();

        String firstName = contactController.getNewContact().getFirstName();
        String lastName = contactController.getNewContact().getLastName();
        String phoneNumber = contactController.getNewContact().getPhoneNumber();
        String emailAddress = contactController.getNewContact().getEmailAddress();

        Contact newContact = new Contact(firstName, lastName, phoneNumber, emailAddress);
        contactsList.add(newContact);

        //or alternatively
        //            Contact newContact = contactController.getNewContact();
        //            contactsList.add(newContact);


    //========================================================================================
    }

}
}

ContactController.java

代码语言:javascript
复制
public class ContactController {


@FXML
private TextField firstNameField;

@FXML
private TextField lastNameField;

@FXML
private TextField phoneNumberFiled;

@FXML
private TextField emailField;


public Contact getNewContact() {

    String firstName = firstNameField.getText();
    String lastName = lastNameField.getText();
    String phoneNumber = phoneNumberFiled.getText();
    String emailAddress = emailField.getText();

    Contact newContact = new Contact(firstName, lastName, phoneNumber, emailAddress);
    return newContact;
}

Contact.java

代码语言:javascript
复制
public class Contact {

private SimpleStringProperty firstName = new SimpleStringProperty("");
private SimpleStringProperty lastName = new SimpleStringProperty("");
private SimpleStringProperty phoneNumber = new SimpleStringProperty("");
private SimpleStringProperty emailAddress = new SimpleStringProperty("");

public Contact() {
}

public Contact(String firstName, String lastName, String phoneNumber, String emailAddress) {
    this.firstName.set(firstName);
    this.lastName.set(lastName);
    this.phoneNumber.set(phoneNumber);
    this.emailAddress.set(emailAddress);
}



public String getFirstName() {
    return firstName.get();
}

public SimpleStringProperty firstNameProperty() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName.set(firstName);
}

public String getLastName() {
    return lastName.get();
}

public SimpleStringProperty lastNameProperty() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName.set(lastName);
}

public String getPhoneNumber() {
    return phoneNumber.get();
}

public SimpleStringProperty phoneNumberProperty() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber.set(phoneNumber);
}

public String getEmailAddress() {
    return emailAddress.get();
}

public SimpleStringProperty emailAddressProperty() {
    return emailAddress;
}

public void setEmailAddress(String emailAddress) {
    this.emailAddress.set(emailAddress);
}


@Override
public String toString() {
    return "Contact{" +
            "firstName=" + firstName +
            ", lastName=" + lastName +
            ", phoneNumber=" + phoneNumber +
            ", emailAddress=" + emailAddress +
            '}';
}
}

mainwindow.fxml

代码语言:javascript
复制
<BorderPane fx:id="mainPanel" fx:controller="sample.Controller"
        xmlns:fx="http://javafx.com/fxml">


<top>
    <MenuBar>
        <menus>
            <Menu text="Contacts">
                <items>
                    <MenuItem text="Add new" onAction="#showAddContactDialog"/>
                </items>
                <items>
                    <MenuItem text="Edit" />
                </items>
                <items>
                    <MenuItem text="Delete"/>
                </items>
                <items>
                    <MenuItem text="Exit"/>
                </items>
            </Menu>
        </menus>
        <menus>
            <Menu text="Info">
                <items>
                    <MenuItem text="About"/>
                </items>

            </Menu>
        </menus>
    </MenuBar>
</top>

<center>
    <TableView fx:id="contactsTable">
        <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
        </columnResizePolicy>
        <columns>
            <TableColumn text="First Name">
                <cellValueFactory>
                    <PropertyValueFactory property="firstName"/>
                </cellValueFactory>
            </TableColumn>

            <TableColumn text="Last Name">
                <cellValueFactory>
                    <PropertyValueFactory property="lastName"/>
                </cellValueFactory>
            </TableColumn>

            <TableColumn text="Phone Number">
                <cellValueFactory>
                    <PropertyValueFactory property="phoneNumber"/>
                </cellValueFactory>
            </TableColumn>

            <TableColumn text="Email">
                <cellValueFactory>
                    <PropertyValueFactory property="emailAddress"/>
                </cellValueFactory>
            </TableColumn>
        </columns>
    </TableView>
</center>
</BorderPane>

addContactDialog.fxml

代码语言:javascript
复制
<DialogPane fx:controller="sample.ContactController" xmlns:fx="http://javafx.com/fxml">
    <headerText>
    Fill in the information for the new Contact
</headerText>
<content>
    <GridPane vgap="10" hgap="10">
        <Label text="First Name: " GridPane.rowIndex="0" GridPane.columnIndex="0"/>
        <TextField fx:id="firstNameField" GridPane.rowIndex="0" GridPane.columnIndex="1"/>

        <Label text="Last Name: " GridPane.rowIndex="1" GridPane.columnIndex="0"/>
        <TextField fx:id="lastNameField" GridPane.rowIndex="1" GridPane.columnIndex="1"/>

        <Label text="Phone Number: " GridPane.rowIndex="2" GridPane.columnIndex="0"/>
        <TextField fx:id="phoneNumberField" GridPane.rowIndex="2" GridPane.columnIndex="1"/>

        <Label text="Notes: " GridPane.rowIndex="3" GridPane.columnIndex="0"/>
        <TextField fx:id="notesField" GridPane.rowIndex="3" GridPane.columnIndex="1"/>

    </GridPane>
</content>
</DialogPane>
EN

回答 2

Stack Overflow用户

发布于 2019-03-12 04:42:34

好了,我发现了错误--我在ContactController.java文件中犯了一个打字错误。它应该有phoneNumberField而不是phoneNumberFiled字符串...

票数 1
EN

Stack Overflow用户

发布于 2019-03-12 02:03:19

你有没有试过这样放.toString String firstName = contactController.getNewContact().getFirstName().toString();

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55107416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档