Java FX ComboBoxTableCell Show in Every Cell [英] Java FX ComboBoxTableCell Show In Every Cell

查看:169
本文介绍了Java FX ComboBoxTableCell Show in Every Cell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含组合单元格的表,您可以从每个行条目中选择一个值。问题是,下拉列表仅显示正在编辑单元格的时间,并且我希望它始终显示在每一行上。这是可能的,如果是这样,怎么样?

I've got a table which has a combo cell which you can select a value from for each row entry. The problem is, the drop down list only shows when the cell is being edited, and I would like it to show on every row at all times. Is this possible and if so, how?

    TableColumn<Product, String> actionsCol = new TableColumn<Product, String>("Exception Reason");
    actionsCol.setCellValueFactory(new PropertyValueFactory("exceptionReason"));
    actionsCol.setCellFactory(ComboBoxTableCell.forTableColumn(exceptionReasons));
    actionsCol.setOnEditCommit(
            new EventHandler<CellEditEvent<Product, String>>() {
                @Override
                public void handle(CellEditEvent<Product, String> t) {
                    ((Product) t.getTableView().getItems().get(t.getTablePosition().getRow())).setExceptionReason(t.getNewValue());
                };
    });
    actionsCol.setEditable(true);


推荐答案

您可以通过实现自己的表格单元格来完成此操作(并设置一个单元工厂来实例化它:)

You can do this by implementing your own table cell (and setting a cell factory to instantiate it):

import java.util.Random;

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class TableWithComboBoxExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();

        TableColumn<Item, String> itemCol = new TableColumn<>("Item");
        itemCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());

        TableColumn<Item, Reason> reasonCol = new TableColumn<>("Reason");
        reasonCol.setCellValueFactory(cellData -> cellData.getValue().importanceProperty());

        reasonCol.setCellFactory(tc -> {
            ComboBox<Reason> combo = new ComboBox<>();
            combo.getItems().addAll(Reason.values());
            TableCell<Item, Reason> cell = new TableCell<Item, Reason>() {
                @Override
                protected void updateItem(Reason reason, boolean empty) {
                    super.updateItem(reason, empty);
                    if (empty) {
                        setGraphic(null);
                    } else {
                        combo.setValue(reason);
                        setGraphic(combo);
                    }
                }
            };
            combo.setOnAction(e -> 
                table.getItems().get(cell.getIndex()).setImportance(combo.getValue()));
            return cell ;
        });

        table.getColumns().add(itemCol);
        table.getColumns().add(reasonCol);

        Random rng = new Random();
        for (int i = 1 ; i <= 100 ; i++) {
            table.getItems().add(new Item("Item "+i, Reason.values()[rng.nextInt(Reason.values().length)]));
        }

        primaryStage.setScene(new Scene(table));
        primaryStage.show();
    }

    public enum Reason { DATA_NOT_OBTAINABLE, IM_JUST_PLAIN_LAZY, I_CANT_BE_BOTHERED }

    public static class Item {
        private final StringProperty name = new SimpleStringProperty();
        private final ObjectProperty<Reason> importance = new SimpleObjectProperty<>();

        public Item(String name, Reason importance) {
            setName(name);
            setImportance(importance);
        }

        public final StringProperty nameProperty() {
            return this.name;
        }


        public final String getName() {
            return this.nameProperty().get();
        }


        public final void setName(final String name) {
            this.nameProperty().set(name);
        }


        public final ObjectProperty<Reason> importanceProperty() {
            return this.importance;
        }


        public final Reason getImportance() {
            return this.importanceProperty().get();
        }


        public final void setImportance(final Reason importance) {
            this.importanceProperty().set(importance);
        }



    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于Java FX ComboBoxTableCell Show in Every Cell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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