JavaFX 中 tableview 单元格中的组合框 [英] ComboBox in a tableview cell in JavaFX

查看:25
本文介绍了JavaFX 中 tableview 单元格中的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Combo Box 添加到我的 Table View:

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

基本上我有一个名为 TableViewTest 的类,它存储名称和描述,我可以在 Table View 中显示这些名称和描述,不用麻烦,但我想做的是添加第三列每个单元格都有一个 Combo Box,以便用户可以为每个人从多个选项中选择一个.

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.

到目前为止,我已经用一些值创建了一个 String 类型的 ObservableList 并将它们添加到一个 ComboBox 对象中.有谁知道我可以将这个 Combo Box 添加到表格中的方法吗?

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);

推荐答案

James_D 的回答效果很好,但需要用户单击该项目才能看到 ComboBox.如果您想在列中使用 ComboBox es,始终显示,您必须使用自定义 cellFactory:

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:

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;
    });

这篇关于JavaFX 中 tableview 单元格中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆