JavaFX TableView获取ChoiceBox的选择值

时间:2016-11-09 14:57:39

标签: javafx tableview

我是javafx编程的新手,并尝试绘制一个表,其中一列更改它显示的输入对象类型。所以对于一个String,例如它应该显示一个TextField,对于Boolean是一个CheckBox,对于一个ObservableList是一个ChoiceBox。 我的表创建如下:

TableView table = new TableView<MyData>();
table.setEditable(true);
TableColumn col = new TableColumn("Option");

col.setCellValueFactory(new PropertyValueFactory<MyData, Object>("OPTIONS"));
col.setCellFactory(new Callback<TableColumn<MyData, Object>, TableCell<MyData, Object>>() {
    public TableCell<MyData, Object> call(TableColumn<MyData, Object> param) {
        return new EditingCell();
    }
});

table_data = FXCollections.observableArrayList();
table.setItems(table_data);
table.getColumns().setAll(col);

MyData对象有一个Object-Property&#34; OPTIONS&#34;用于填充列。 我使用的EditingCell-Class看起来像这样:

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;

class EditingCell extends TableCell<MyData, Object> {
private TextField textField;
private CheckBox checkBox;
private ChoiceBox<String> choiceBox;


    public EditingCell() {}

    public void startEdit() {
        if (!isEmpty()) {
            super.startEdit();
            if (getItem() instanceof Boolean) {
                createCheckBox();
                setText(null);
                setGraphic(checkBox);
            } 
            if (getItem() instanceof String){
                createTextField();
                setText(null);
                setGraphic(textField);
                textField.selectAll();
            }  
            if (getItem() instanceof ObservableList){
                createChoiceBox();
                setText(null);
                setGraphic(choiceBox);              
            }
        }
    }

    public void cancelEdit() {
        super.cancelEdit();
        if (getItem() instanceof Boolean) {
            setText(null);
        } 
        if (getItem() instanceof String){
            setText((String) getItem());
        }  
        if (getItem() instanceof ObservableList){
            if (choiceBox==null) 
                setText(null);
            else 
                setText(choiceBox.getSelectionModel().getSelectedItem());
        }
    }

    public void updateItem(Object item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (getItem() instanceof Boolean) {
                    if (checkBox != null) {
                        checkBox.setSelected(getBoolean());
                    }
                    setText(null);
                    setGraphic(checkBox);
                } 
                if (getItem() instanceof String){
                    if (textField != null) {
                        textField.setText(getString());
                    }
                    setText(null);
                    setGraphic(textField);
                }
                if (getItem() instanceof ObservableList){
                    if (choiceBox != null){
                        choiceBox.setItems(getList());
                    }
                    setText(null);
                    setGraphic(choiceBox);
                }
            } else {
                setText(getString());
                if (getItem() instanceof Boolean) {
                    if (checkBox==null) checkBox = new CheckBox();
                    setGraphic(checkBox);
                    setText(null);
                } 
                if (getItem() instanceof String){
                    if (textField==null) textField = new TextField();
                    setGraphic(textField);
                }
                if (getItem() instanceof ObservableList){
                    if (choiceBox==null) choiceBox = new ChoiceBox<String>();
                    setGraphic(null);
                    setText(choiceBox.getSelectionModel().getSelectedItem());
                }
            }
        }
    }

    private void createTextField() {
      textField = new TextField(getString());
      textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
      textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
          if (!newValue) {
            commitEdit(textField.getText());
          }
        }
      });
    }

    private void createCheckBox() {
      checkBox = new CheckBox();
      checkBox.setSelected(getBoolean());
      checkBox.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
      checkBox.focusedProperty().addListener(new ChangeListener<Boolean>() {
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
          if (!newValue) {
            commitEdit(checkBox.isSelected());
          }
        }
      });
    }


    private void createChoiceBox(){
        choiceBox = new ChoiceBox<String>();
        choiceBox.setItems((ObservableList<String>) getItem());
        choiceBox.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
        choiceBox.focusedProperty().addListener(new ChangeListener<Boolean>(){
            public void changed(ObservableValue<? extends Boolean> Observable, Boolean oldValue, Boolean newValue) {
                if (!newValue){
                    commitEdit(choiceBox.getItems());
                }
            }
        });
    }

    private String getString() {
        return getItem() == null ? "" : getItem().toString();
    }

    private Boolean getBoolean() {
        return getItem() == null ? false : (Boolean) getItem();
    }



    private ObservableList<String> getList(){
        return getItem() == null ? FXCollections.observableArrayList("") : (ObservableList<String>) getItem();

    }
} 

所以到目前为止一切正常:创建表并用数据填充它,但是当我尝试访问&#34; choiceBox-typed&#34;使用编辑细胞(索引1是选择框 - 行)

    Object a = col.getCellData(1);
    Object b = col.getCellObservableValue(1);
    System.out.println("a Class: "+a.getClass().getName());
    System.out.println("a Value: "+a.toString());       
    System.out.println("b Class: "+b.getClass().getName());
    System.out.println("b Value: "+b.toString());

我只获得用作choiceBox的输入的List-Objects:

a Class: com.sun.javafx.collections.ObservableListWrapper
a Value: [o1, o2]
b Class: javafx.beans.property.SimpleListProperty
b Value: ListProperty [value: [o1, o2]]

我是如何获得ChoiceBox的选定项目的? 提前谢谢。

0 个答案:

没有答案