0

I have following basic code in JavaFx. If I want to change the name of PropertyValueFactory("id") to let's say "rid" it does not fill my table anymore. Even if I change the SimpleLongProperty object to rid too. Do you know where the "id" is referencing / pointing to?

public class Main_Controller implements Initializable{

    public class Item {
        public SimpleLongProperty id = new SimpleLongProperty();

        public Long getId() {
            return id.get();
        }
    }

    // The table and columns
    @FXML TableView<Item> itemTbl;
    @FXML TableColumn itemIdCol;
    @FXML Button add_Button;

    // The table's data
    ObservableList<Item> data;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // Set up the table data
        itemIdCol.setCellValueFactory(
            new PropertyValueFactory<Item,Long>("id")
        );

        data = FXCollections.observableArrayList();
        itemTbl.setItems(data);
    }    

    static long nextId = 1;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        Item item = new Item();
        item.id.setValue(nextId++);
        data.add(item);
    }
}

Code With changed PropertyValueFactory:

public class Main_Controller implements Initializable{

    public class Item {
        public SimpleLongProperty rid = new SimpleLongProperty();

        public Long getId() {
            return rid.get();
        }
    }

    // The table and columns
    @FXML TableView<Item> itemTbl;
    @FXML TableColumn itemIdCol;
    @FXML Button add_Button;

    // The table's data
    ObservableList<Item> data;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // Set up the table data
        itemIdCol.setCellValueFactory(
            new PropertyValueFactory<Item,Long>("rid")
        );

        data = FXCollections.observableArrayList();
        itemTbl.setItems(data);
    }    

    static long nextId = 1;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        Item item = new Item();
        item.rid.setValue(nextId++);
        data.add(item);
    }
}
user3721905
  • 35
  • 1
  • 10

1 Answers1

0

You need a method in your Item class called

public Long getRid(){return rid.get();}

or

public SimpleLongProperty ridProperty(){return rid;}

Note, it's not the name of the variable, which would normally be private, but the name of the getter that matters.

Javadoc link

brian
  • 10,619
  • 4
  • 21
  • 79