2

I'm trying to populate a TableView with data from an ObservableList. I've done this before but for some reason I can't get it to work now. I don't get any exceptions or anything but it simply doesn't add anything to the table.

this question is similar but I identified another issue caused by JPA which made it so the mentioned constructor for Modification is never executed. Instead JPA does its magic to assign the values.

Here's my code - I snipped code that was irrelevant to the question:

FXML

<TableView fx:id="tblModifications" layoutX="14.0" layoutY="14.0" prefHeight="545.0" prefWidth="512.0">
    <columns>
        <TableColumn prefWidth="75.0" text="%itemNumber" fx:id="colModArt"/>
        <TableColumn prefWidth="75.0" text="%name" fx:id="colModName"/>
        <TableColumn prefWidth="75.0" text="%amount" fx:id="colModAmount" />
    </columns>
</TableView>

Main.java

public class Main extends Application implements Initializable {
    private TableView<Modification> tblModifications;
    private TableColumn<ObservableList<Modification>, String> colModArt;
    private TableColumn<ObservableList<Modification>, String> colModName;
    private TableColumn<ObservableList<Modification>, Integer> colModAmount;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1){
        colModArt.setCellValueFactory(new PropertyValueFactory<>("barcodeProperty"));
        colModName.setCellValueFactory(new PropertyValueFactory<>("nameProperty"));
        colModAmount.setCellValueFactory(new PropertyValueFactory<>("amountProperty"));

        admin = Administration.getInstance();

        tblModifications.setItems(admin.observableModifications); // This list is populated with correct data, I tested.
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        resources = ResourceBundle.getBundle("strings", new Locale("NL"));
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("main.fxml"), resources);
        primaryStage.setTitle(resources.getString("title"));
        primaryStage.setScene(new Scene(root, 1280, 1024));
        primaryStage.show();
    }
}

Modification.java

public class Modification {
    public SimpleStringProperty barcodeProperty;
    public SimpleStringProperty nameProperty;
    public SimpleIntegerProperty amountProperty;
    private int id;
    private Seller seller;
    private Product product;
    private int amount;
    private Boolean accepted;

    public Modification(int id, Seller seller, Product product, int amount) {
        this.id = id;
        this.seller = seller;
        this.product = product;
        this.amount = amount;
        this.accepted = false;

        barcodeProperty.set(String.valueOf(product.getBarcode()));
        nameProperty.set(product.getName());
        amountProperty.set(amount);
    }
}

Any help in resolving this issue would be greatly appreciated!

Community
  • 1
  • 1
Cas
  • 758
  • 14
  • 36
  • 2
    Possible duplicate of [Javafx PropertyValueFactory not populating Tableview](http://stackoverflow.com/questions/17035478/javafx-propertyvaluefactory-not-populating-tableview) – DVarga Jun 14 '16 at 11:54
  • 1
    And as addition: `private TableColumn, String> colModArt` shall be `private TableColumn colModArt` - this is valid for each `TableColumn` definition. – DVarga Jun 14 '16 at 11:58
  • @DVarga Thanks! I did indeed change the names in the `PropertyValueFactory`'s which made it get the correct info. I then realised that the constructor is never called since JPA creates the objects with another constructor. I simply created a get method for the values instead which fixed it all. – Cas Jun 14 '16 at 12:00

1 Answers1

1

It seems this issue was caused in part by JPA.

First, I used the incorrect field names in the PropertyValueFactory's. I had to remove the Property part from them because apparently this is added automatically (reflection?)

Second, the JPA implementation never executed the Modification constructor so the SimpleStringProperty never got its value. I resolved this by writing my own getters for these values: getProductName and getProductBarcode

Cas
  • 758
  • 14
  • 36
  • It's probably a good thing your constructor wasn't invoked, as it would throw a `NullPointerException` if it were... To understand how the `PropertyValueFactory` works, read the [documentation](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/cell/PropertyValueFactory.html) – James_D Jun 14 '16 at 14:23