ComboBox in a tableview cell in JavaFX

noobCoder

I'm trying to a add a Combo Box to my Table View:

Basically I have a class called TableViewTest that stores a name and a description, I can display theses names and descriptions in a Table View no bother, but what I want to do is add a third column with each cell having a Combo Box so that the user can select one from a number of options for each person.

So far I have created an ObservableList of type String with some values and added them to a ComboBox object. Does anyone know a way for me to add this Combo Box to the table?

Also bear in mind this code is pretty rough and I'm just trying to get something working and I'll be refactoring the code at a later date.

ObservableList<TableViewTest> products = FXCollections.observableArrayList();

    for(int i = 0; i < b.length; i++){

        // random String values
        products.add(new TableViewTest(b[i], a[i]));
    }

ObservableList<String> options = FXCollections.observableArrayList(
                                "1",
                                "2",
                                "3"
                                );
final ComboBox comboBox = new ComboBox(options);

TableColumn<TableViewTest, String> nameColumn = new TableColumn<> ("Name");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<TableViewTest, String>("name"));

                //price Column
                //Stock Column
TableColumn<TableViewTest, String> StockColumn = new TableColumn<> ("Stock");
StockColumn.setMinWidth(150);
StockColumn.setCellValueFactory(new PropertyValueFactory<TableViewTest, String>("description"));


TableColumn<Object,ComboBox> PriceColumn;
PriceColumn = new TableColumn<>("Source");
PriceColumn.setMinWidth(150);
   //PriceColumn.setCellValueFactory(new PropertyValueFactory<>
   //(options));

   //PriceColumn.setCellFactory(ComboBoxTableCell.forTableColumn(new 
   //DefaultStringConverter(), options));


   //PriceColumn.setCellFactory(ComboBoxTableCell.forTableColumn( 
   //comboBox));

TableView<TableViewTest> table = new TableView<>();

table.setItems(products);
table.getColumns().addAll(nameColumn, StockColumn, PriceColumn);
fabian

James_D's answer works well, but requires the user to click on the item to see the ComboBox. If you want to have ComboBoxes in a column, that are always shown, you have to use a custom cellFactory:

Example:

public class TableViewTest {

    ...

    private final StringProperty option = new SimpleStringProperty();

    public String getOption() {
        return option.get();
    }

    public void setOption(String value) {
        option.set(value);
    }

    public StringProperty optionProperty() {
        return option;
    }
    
}
    TableColumn<TableViewTest, StringProperty> column = new TableColumn<>("option");
    column.setCellValueFactory(i -> {
        final StringProperty value = i.getValue().optionProperty();
        // binding to constant value
        return Bindings.createObjectBinding(() -> value);
    });
    
    column.setCellFactory(col -> {
        TableCell<TableViewTest, StringProperty> c = new TableCell<>();
        final ComboBox<String> comboBox = new ComboBox<>(options);
        c.itemProperty().addListener((observable, oldValue, newValue) -> {
            if (oldValue != null) {
                comboBox.valueProperty().unbindBidirectional(oldValue);
            }
            if (newValue != null) {
                comboBox.valueProperty().bindBidirectional(newValue);
            }
        });
        c.graphicProperty().bind(Bindings.when(c.emptyProperty()).then((Node) null).otherwise(comboBox));
        return c;
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaFX TableView updateItem slow for ComboBox

From Dev

JavaFX TableView ComboBox String Converter

From Dev

JavaFX TableView edit integer cell

From Dev

JavaFX horizontal scrollbar into cell of TableView

From Dev

JavaFX Populating TableView by using ComboBox selction

From Dev

How to add a click event to a tableview cell in javafx

From Dev

JavaFX TableView how to get cell's data?

From Dev

JavaFx Set Tableview Cell Background Color Dynamically

From Dev

JavaFX TableView change selected cell colour

From Dev

JavaFX : Coloring TableView row and formatting the cell text

From Dev

JavaFX - How to refer to a cell's neighbor in tableview

From Dev

JavaFX 2 TableView how to update cell when object is changed

From Dev

How to add a tooltip to a TableView header cell in JavaFX 8

From Dev

How to make a JavaFX TableView cell editable without first pressing Enter?

From Dev

How can I right click on a cell in tableview in Javafx (fxml)?

From Dev

JavaFX TableView: format one cell based on the value of another in the row

From Dev

javafx Tableview cell index/editing bug with duplicated entries

From Dev

JavaFX: TableView Cell pixelates ImageView - How do I undo transform?

From Dev

JavaFX 2 TableView how to update cell when object is changed

From Dev

JavaFX - TableView - Populate cell with text from multiple properties

From Dev

Error Trying to get cell data value JavaFX TableView

From Dev

Javafx: Detect ALL changes made to tableView including addition/deletion of table rows and cell edits on any table rows

From Dev

TableView Cell click to another TableView

From Dev

javafx create ComboBox TableCell

From Dev

Javafx: Data is not visible in ComboBox

From Dev

JavaFX ComboBox Image

From Dev

JavaFX ComboBox containing Bytes

From Dev

AutoComplete ComboBox in JavaFX

From Dev

"No select item" on JavaFX combobox?

Related Related

  1. 1

    JavaFX TableView updateItem slow for ComboBox

  2. 2

    JavaFX TableView ComboBox String Converter

  3. 3

    JavaFX TableView edit integer cell

  4. 4

    JavaFX horizontal scrollbar into cell of TableView

  5. 5

    JavaFX Populating TableView by using ComboBox selction

  6. 6

    How to add a click event to a tableview cell in javafx

  7. 7

    JavaFX TableView how to get cell's data?

  8. 8

    JavaFx Set Tableview Cell Background Color Dynamically

  9. 9

    JavaFX TableView change selected cell colour

  10. 10

    JavaFX : Coloring TableView row and formatting the cell text

  11. 11

    JavaFX - How to refer to a cell's neighbor in tableview

  12. 12

    JavaFX 2 TableView how to update cell when object is changed

  13. 13

    How to add a tooltip to a TableView header cell in JavaFX 8

  14. 14

    How to make a JavaFX TableView cell editable without first pressing Enter?

  15. 15

    How can I right click on a cell in tableview in Javafx (fxml)?

  16. 16

    JavaFX TableView: format one cell based on the value of another in the row

  17. 17

    javafx Tableview cell index/editing bug with duplicated entries

  18. 18

    JavaFX: TableView Cell pixelates ImageView - How do I undo transform?

  19. 19

    JavaFX 2 TableView how to update cell when object is changed

  20. 20

    JavaFX - TableView - Populate cell with text from multiple properties

  21. 21

    Error Trying to get cell data value JavaFX TableView

  22. 22

    Javafx: Detect ALL changes made to tableView including addition/deletion of table rows and cell edits on any table rows

  23. 23

    TableView Cell click to another TableView

  24. 24

    javafx create ComboBox TableCell

  25. 25

    Javafx: Data is not visible in ComboBox

  26. 26

    JavaFX ComboBox Image

  27. 27

    JavaFX ComboBox containing Bytes

  28. 28

    AutoComplete ComboBox in JavaFX

  29. 29

    "No select item" on JavaFX combobox?

HotTag

Archive