NeiW
NeiW

Reputation: 49

JavaFX TableVie .setCellValueFactory to String without object

I have a table which populates a list of objects, in one of the columns I have a comboBoxColumn. Although the combobox column is not part of the object class, I just want it to display a String in the column.

How would i do this? Without setting the object class.

comboBoxColumn.setCellValueFactory(new **Just String**);

Here is the code

@FXML
TableView<Dog> BookDogTableView;

@FXML
TableColumn<Dog, String> BookDogNameCol;

@FXML
TableColumn<Dog, String> BookDogBreedCol;

@FXML
TableColumn<Dog, Integer> BookDogAgeCol;

@FXML
TableColumn<String, String> BookDogSelectRunCol;


BookDogNameCol
            .setCellValueFactory(new PropertyValueFactory<Dog, String>(
                    "dogName"));

    BookDogBreedCol
            .setCellValueFactory(new PropertyValueFactory<Dog, String>(
                    "breed"));

    BookDogAgeCol
            .setCellValueFactory(new PropertyValueFactory<Dog, Integer>(
                    "age"));

    BookDogSelectDogPane.setCellValueFactory(new **Just String**);

        BookDogSelectRunCol.setCellFactory(ComboBoxTableCell.forTableColumn(runOptions));

    BookDogTableView.setItems(customersDogs);

Upvotes: 1

Views: 2240

Answers (1)

Grochni
Grochni

Reputation: 1851

I'm not completely sure if I get you right. If you just want to display the same String in this column in every row, you would do the following thing:

@FXML
TableColumn<Dog, String> BookDogSelectRunCol;
...
BookDogSelectRunCol.setCellValueFactory(x -> new SimpleObjectProperty<>("Just String"));

This will pass "Just String" to the cell. If you just want the String to be displayed in the cell, you're done. If you want to display a ComboBox as you mentioned and use the String for it in some way, you'll have to use the setCellFactory method.

BookDogSelectRunCol.setCellFactory(x -> new TableCell<>() {
        @Override
        protected void updateItem(String item, boolean empty) {
            // Your code ...
        }
    });

The value of item would be "Just String" in this example.

But, as James_D already pointed out, the first type parameter passed to a TableColumn has to be the same type as passed to the TableView.

Upvotes: 1

Related Questions