Dan
Dan

Reputation: 89

Setting the text in the textfield isn't working

I am trying to get the textfield to store a persons name but it keeps appearing as blank, what I currently have is as follows.

In my FXML file I have this label and textfield

<Label layoutX="156.0" layoutY="82.0" prefHeight="17.0" prefWidth="42.0" text="Name:" />

<TextField fx:id="nameTf" text="${controller.customer.name}" layoutX="204.0" layoutY="78.0" />

inside my controller for this FXML I have.

public class ServeController {

@FXML private TextField nameTf;
@FXML private TextField phoneTf;

private String getName() { return nameTf.getText(); }
private double getPhone () { return Double.parseDouble (phoneTf.getText());
}

public void setName(String name) {
    nameTf.setText(name);
}

private void setPhone(String phone) {
    nameTf.setText(phone);
}

@FXML private void initialize() {
}

}

and I call the setter from another Controller to set the name like so.

ServeController serve = new ServeController();

@FXML private void serveCustomer (ActionEvent event) throws Exception {
    serve.setName(customers.get(namelistview.getSelectionModel().getSelectedIndex()).getName());

customers is a ObservableList, the getName() function returns the name as a string (I have tested this and it works).

Upvotes: 0

Views: 340

Answers (1)

Dan
Dan

Reputation: 89

The main issue was as James pointed out, the fact that I was calling the setter on a object instead of the actual controller, by inserting the following code

ServeController controller = fxmlLoader.getController();

instead of

ServeController serve = new ServeController();

and calling the setters by referencing controller, everything seemed to work fine.

Thanks for your help James :)

Upvotes: 1

Related Questions