0

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).

Dan
  • 89
  • 8
  • 1. I don't see any property called `customer` in your controller. What are you expecting the `text="..."` in the FXML to do? 2. You are not calling `setName(...)` on the controller, you are calling it on an object you created that is another instance of the controller class. So it won't change the text in the text field. – James_D Jun 05 '17 at 03:09
  • I am expecting the FXML value to change into the customer name, currently customer is public Customer(Kitchen kitchen, String phone, String name) and how would i go about fixing the second issue? – Dan Jun 05 '17 at 03:13
  • But there is no `customer` property in the controller? Or did you just omit it? To fix the second issue, you need to call `setName()` on the actual controller. You can get the controller from the FXML Loader when you load the corresponding FXML and display it, but you haven't explained the relationship between the two controllers, so no one can tell you how to do that exactly. – James_D Jun 05 '17 at 03:14
  • See if https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310 helps – James_D Jun 05 '17 at 03:16
  • Sorry, the relationship between the 2 is as follows, The main controller pizzeriaController opens up a new instance of the Serve XML when a button is clicked, the Pizzeria contains a observablelist of customers which is printed out in a listview, once a user clicks one of the names from the list view and clicks the button, I want their name to appear in the newly opened Serve XML. I'll have a look at the link you provided thanks – Dan Jun 05 '17 at 03:21

1 Answers1

1

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 :)

Dan
  • 89
  • 8