0

I have a weird problem in using TableView

my TableView Is showing blank line, what am I missing?

Controller.java

public class Controller {


@FXML private TableView<PersonalInfo> personalInfo = new TableView<>();
@FXML private TableColumn<PersonalInfo,String> tvId;


@FXML
public void initialize() {

    tvId.setCellValueFactory(new PropertyValueFactory<>("Id"));

}

public void btnShow(ActionEvent actionEvent) {
    List<PersonalInfo> participantList = new ArrayList<>();

    participantList.add(new PersonalInfo("10") );

    personalInfo.getItems().clear();
    personalInfo.getItems().addAll(participantList);
}}

PersonalInfo:

public class PersonalInfo {
private String pId;

public PersonalInfo(String pId) {
    this.pId = pId;
}
public PersonalInfo() {
}

public PersonalInfo setpId(String pId) {
    this.pId = pId;
    return this;
}

public String getpId() {
    return pId;
}}

sample.fxml :

   <children>
  <TableView fx:id="personalInfo">
    <columns>
      <TableColumn fx:id="tvId" prefWidth="75.0" text="Id" />
    </columns>
  </TableView>
  <Button mnemonicParsing="false" onAction="#btnShow" text="Show" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" /></children>

above code only show a blank row what I can't see the content? enter image description here

Edit: Thanks, guys I found where I made a mistake as Slaw said the problem is I pass "Id" to PropertyValueFactory and in PersonalInfo.java file my getter and setter function is getpId and setpId so we have two option to fix it.

First:

we could change "Id" to "pId" and our setter and getter to getPId and setPId(because of java name convention)

Second :

we could change setter and getter function to getId and setId

then everything is fine and works perfectly.

  • 5
    You're creating a `new TableView` in your controller. This is replacing the one you've defined in your FXML, so when you add items to the `TableView`, it's being added to this new one and not the one shown in the stage. Remove the `= new TableView<>()` from your controller. – Zephyr Nov 24 '21 at 21:24
  • 5
    @Zephyr Field initialization happens at instantiation time, which occurs before the FXML fields are injected. But you're correct that `= new TableView<>()` should be removed. – Slaw Nov 24 '21 at 21:55
  • 4
    You're using `PropertyValueFactory` and pass it `"Id"`. Based on JavaBean naming conventions, that causes it to look for a method named `getId()`. Your `PersonalInfo` class does not have such a method. Most likely you meant to pass `"pId"` instead of `"Id"`. – Slaw Nov 24 '21 at 21:59
  • 2
    Lambda's are, in most cases, [superior to using a PropertyValueFactory](https://stackoverflow.com/questions/38049734/java-setcellvaluefactory-lambda-vs-propertyvaluefactory-advantages-disadvant) because they allow you to catch issues at compile time rather than runtime failures like this. – jewelsea Nov 24 '21 at 23:44
  • 2
    In addition to what Slaw said, your accessor method names are not correct. See https://stackoverflow.com/questions/65785787/javafx-tableview-not-showing-data-something-wrong-with-getter-setters. – VGR Nov 25 '21 at 00:01
  • 1
    Note that you __do not__ have a pair of getter/setter for either `pId` or `id` (at least not in the code you are showing): a real setter by convention must have a return type of void. Also note that given a getter of name `getId`, the property name is `id` (not `Id`) - case matters (though you do get away with the upper case first because PropertyValueFactory is lenient at that location :) – kleopatra Nov 25 '21 at 12:34

0 Answers0