如何编辑JavaFX 2中ComboBoxTableCell的默认呈现行为? [英] How to edit default render behaviour of ComboBoxTableCell in JavaFX 2?

查看:1677
本文介绍了如何编辑JavaFX 2中ComboBoxTableCell的默认呈现行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,ComboBoxTableCell在未编辑时作为标签呈现。我想改变这种行为,以便它将被渲染为一个ComboBox所有的时间。但我找不出怎么样。

By default ComboBoxTableCell is rendered as a label when not being edited. I want to change that behaviour so that it would be rendered as a ComboBox all the time. However I couldn't find out how.

基本上我有一个TableView。某些列的类型是ComboBox。我想要ComboBox节点总是显示。

Basically I have a TableView. Some of the columns are of type ComboBox. I want ComboBox node is shown always.

varTypeCol.setCellValueFactory(new PropertyValueFactory<GlobalVariable, String>("varType"));
varTypeCol.setCellFactory(new Callback<TableColumn<GlobalVariable, String>, TableCell<GlobalVariable, String>>() {

      @Override
      public TableCell<GlobalVariable, String> call(TableColumn<GlobalVariable, String> p) {
            return new ComboBoxTableCell<GlobalVariable, String>(FXCollections.observableArrayList("String", "Integer"));
      }
});


推荐答案

您需要实现自己的 TableCell 。类似

You need to implement your own TableCell. Something like

public class LiveComboBoxTableCell<S,T> extends TableCell<S, T> {

    private final ComboBox<T> comboBox ;

    public LiveComboBoxTableCell(ObservableList<T> items) {
        this.comboBox = new ComboBox<>(items);

        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

        comboBox.valueProperty().addListener(new ChangeListener<T>() {
            @Override
            public void changed(ObservableValue<? extends T> obs, T oldValue, T newValue) {
                // attempt to update property:
                ObservableValue<T> property = getTableColumn().getCellObservableValue(getIndex());
                if (property instanceof WritableValue) {
                    ((WritableValue<T>) property).setValue(newValue);
                }
            }
        });
    }

    @Override
    public void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setGraphic(null);
        } else {
            comboBox.setValue(item);
            setGraphic(comboBox);
        }
    }
}

这里是一个SSCCE: / p>

Here is an SSCCE:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.WritableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class LiveComboBoxTableCellExample extends Application {

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

        TableColumn<Item, String> typeCol = new TableColumn<>("Type");
        typeCol.setCellValueFactory(new PropertyValueFactory<>("type"));
        TableColumn<Item, String> valueCol = new TableColumn<>("Value");
        valueCol.setCellValueFactory(new PropertyValueFactory<>("value"));

        table.getColumns().add(typeCol);
        table.getColumns().add(valueCol);

        typeCol.setCellFactory(new Callback<TableColumn<Item, String>, TableCell<Item, String>>() {

            @Override
            public TableCell<Item, String> call(TableColumn<Item, String> param) {
                return new LiveComboBoxTableCell<>(FXCollections.observableArrayList("String", "Integer"));
            }

        });

        for (int i = 1 ; i <= 10; i++) {
            table.getItems().add(new Item( (i % 2 == 0 ? "String" : "Integer"), "Item "+i));
        }

        Button checkButton = new Button("Run check");
        checkButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                for (Item item : table.getItems()) {
                    System.out.println(item.getType() + " : "+item.getValue());
                }
                System.out.println();
            }

        });

        BorderPane root = new BorderPane();
        root.setCenter(table);
        root.setBottom(checkButton);
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static class LiveComboBoxTableCell<S,T> extends TableCell<S, T> {

        private final ComboBox<T> comboBox ;

        public LiveComboBoxTableCell(ObservableList<T> items) {
            this.comboBox = new ComboBox<>(items);

            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

            comboBox.valueProperty().addListener(new ChangeListener<T>() {
                @Override
                public void changed(ObservableValue<? extends T> obs, T oldValue, T newValue) {
                    // attempt to update property:
                    ObservableValue<T> property = getTableColumn().getCellObservableValue(getIndex());
                    if (property instanceof WritableValue) {
                        ((WritableValue<T>) property).setValue(newValue);
                    }
                }
            });
        }

        @Override
        public void updateItem(T item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setGraphic(null);
            } else {
                comboBox.setValue(item);
                setGraphic(comboBox);
            }
        }
    }

    public static class Item {
        private final StringProperty type = new SimpleStringProperty();
        private final StringProperty value = new SimpleStringProperty();

        public Item(String type, String value) {
            setType(type);
            setValue(value);
        }

        public final StringProperty typeProperty() {
            return this.type;
        }

        public final String getType() {
            return this.typeProperty().get();
        }

        public final void setType(final String type) {
            this.typeProperty().set(type);
        }

        public final StringProperty valueProperty() {
            return this.value;
        }

        public final String getValue() {
            return this.valueProperty().get();
        }

        public final void setValue(final String value) {
            this.valueProperty().set(value);
        }


    }

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

这篇关于如何编辑JavaFX 2中ComboBoxTableCell的默认呈现行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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