user3023715
user3023715

Reputation: 1619

Populating table data with javafx using fxml

It has been so difficult for me to try and get an answer for this. Oracle doesn't document on their website for some reason?! It has been asked on stackoverflow already (JavaFX: TableView(fxml) filling with data, and JavaFX FXML table; why won't my data display), but when i try to recreate with the answers given, I am still unable to get it to work. I would be extremely grateful if someone would be able to help! I'll give out all the code of have put together.

public class Heroes extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            AnchorPane page = FXMLLoader.load(Heroes.class.getResource("FXMLDocument.fxml"));

            Scene scene = new Scene(page);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Sample Window"); 
            primaryStage.setResizable(false); 
            primaryStage.show();
        } catch (Exception e) {
        }
    }
}

public class FXMLDocumentController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML
    TableView<Table> tableID;
    @FXML
    TableColumn<Table, Integer> tID;
    @FXML
    TableColumn<Table, String> tName;
    @FXML
    TableColumn<Table, String> tDate;
    @FXML
    TableColumn<Table, String> tPrice;
    int iNumber = 1;
    ObservableList<Table> data =
            FXCollections.observableArrayList(
            new Table(iNumber++, "Superman", "12/02/2013", "20"),
            new Table(iNumber++, "Batman", "2/02/2013", "40"),
            new Table(iNumber++, "Aquaman", "1/02/2013", "100"),
            new Table(iNumber++, "The Flash", "12/02/2013", "16"));

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // System.out.println("called");

        tID.setCellValueFactory(new PropertyValueFactory<Table, Integer>("rID"));
        tName.setCellValueFactory(new PropertyValueFactory<Table, String>("rName"));
        tDate.setCellValueFactory(new PropertyValueFactory<Table, String>("rDate"));
        tPrice.setCellValueFactory(new PropertyValueFactory<Table, String>("rPrice"));
        tableID.setItems(data);

    }
}

public class Table {

    SimpleIntegerProperty rID;
    SimpleStringProperty rName;
    SimpleStringProperty rDate;
    SimpleStringProperty rPrice;

    public Table(int rID, String rName, String rDate, String rPrice) {
        this.rID = new SimpleIntegerProperty(rID);
        this.rName = new SimpleStringProperty(rName);
        this.rDate = new SimpleStringProperty(rDate);
        this.rPrice = new SimpleStringProperty(rPrice);
        System.out.println(rID);
        System.out.println(rName);
        System.out.println(rDate);
        System.out.println(rPrice);
    } 

    public Integer getrID() {
        return rID.get();
    }

    public void setrID(Integer v) {
        this.rID.set(v);
    }

    public String getrDate() {
        return rDate.get();
    }

    public void setrDate(String d) {
        this.rDate.set(d);
    }

    public String getrName() {
        return rName.get();
    }

    public void setrName(String n) {
         this.rDate.set(n);
    }

    public String getrPrice() {
        return rPrice.get();
    }

    public void setrPrice(String p) {
       this.rPrice.set(p); 
    }
}
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="heroes.FXMLDocumentController">
  <children>
    <SplitPane dividerPositions="0.535175879396985" focusTraversable="true" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <items>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
          <children>
            <TableView fx:id="tableID" prefHeight="210.0" prefWidth="598.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
              <columns>
                <TableColumn minWidth="20.0" prefWidth="40.0" text="ID" fx:id="tID" >
                    <cellValueFactory>
                       <PropertyValueFactory property="tID" />
                     </cellValueFactory>
                </TableColumn>
                <TableColumn prefWidth="100.0" text="Date" fx:id="tDate" >
                    <cellValueFactory>
                       <PropertyValueFactory property="tDate" />
                    </cellValueFactory>
                 </TableColumn>
                <TableColumn prefWidth="200.0" text="Name" fx:id="tName" >
                    <cellValueFactory>
                       <PropertyValueFactory property="tName" />
                     </cellValueFactory>
                </TableColumn>
                <TableColumn prefWidth="75.0" text="Price" fx:id="tPrice" >
                     <cellValueFactory>
                       <PropertyValueFactory property="tPrice" />
                     </cellValueFactory>
                </TableColumn>
              </columns>
            </TableView>
          </children>
        </AnchorPane>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
      </items>
    </SplitPane>
  </children>
</AnchorPane>

Upvotes: 3

Views: 3142

Answers (1)

Knut Arne Vedaa
Knut Arne Vedaa

Reputation: 15742

The PropertyValueFactory tries to find your data values in two ways:

1) Looking for a method e.g. nameProperty where name is what you specify in the constructor, in your example e.g. "rID".

Thus you need to a methods named like this (name of the field + "Property"):

public IntegerProperty rIDProperty() {
    return rID;
}

And similar for your other properties.

2) If these aren't present, it looks for methods named e.g. getName where Name is what you specifiy in the constructor with the first letter capitalized (following the Java Beans convention).

In your example you don't have 1) but you seem to have 2) - but you haven't capitalized the first letter of the property name in the getters and setters!

So change these to e.g.

public Integer getRID() {
    return rID.get();
}

And similar.

You need either 1) or 2) but it would be good practice to have both.

Also note that the problem is not related to FXML in any way.

Upvotes: 1

Related Questions