ControlsFX SpreadsheetView:如何将 SpreadsheetCell 设置为仅图形?

ControlsFX SpreadsheetView: How to set SpreadsheetCell to graphic only?

我已经创建了自己的 SpreadsheetCellEditor 来显示 ColorPicker,但我不想 return 返回 ColorPicker.getValue().toString,我想 [=17] =] 一个标签 所选值的背景颜色。我搜索了 setContentDisplay(ContentDisplay.GRAPHIC_ONLY) 但它似乎不存在于 SpreadsheetCell 上。 那么我该如何实现呢? 到目前为止,这是我的实现,

public class comboboxCellEditor extends SpreadsheetCellEditor {
    private final ColorPicker colorPicker = new ColorPicker();
    private EventHandler<KeyEvent> eh;
    private ChangeListener<Color> cl;
    private boolean ending = false;

    public comboboxCellEditor(SpreadsheetView view) {
        super(view);

    }

    @Override
    public void startEdit(Object value) {
        if (value instanceof Color) {
            this.colorPicker.setValue((Color) value);
        }

        attachEnterEscapeEventHandler();
        this.colorPicker.show();
        this.colorPicker.requestFocus();

    }


    @Override
    public Control getEditor() {
        return colorPicker;
    }


    public String getControlValue() {

        return this.colorPicker.getValue().toString();

    }

    @Override
    public void end() {
        if (this.colorPicker.isShowing()) {
            this.colorPicker.hide();
        }

        this.colorPicker.removeEventFilter(KeyEvent.KEY_PRESSED, this.eh);
        this.colorPicker.valueProperty().removeListener(this.cl);
    }

    private void attachEnterEscapeEventHandler() {
        this.eh =
                new EventHandler<KeyEvent>() {
                    public void handle(KeyEvent t) {
                        if (t.getCode() == KeyCode.ENTER) {
                            comboboxCellEditor.this.ending = true;
                            comboboxCellEditor.this.endEdit(true);
                            comboboxCellEditor.this.ending = false;
                        } else if (t.getCode() == KeyCode.ESCAPE) {
                            comboboxCellEditor.this.endEdit(false);
                        }

                    }
                };
        this.colorPicker.addEventFilter(KeyEvent.KEY_PRESSED, this.eh);
        this.cl = new ChangeListener<Color>() {
            public void changed(ObservableValue<? extends Color> observable, Color oldValue, Color newValue) {
                if (!comboboxCellEditor.this.ending)
                    comboboxCellEditor.this.endEdit(true);

            }
        };
        this.colorPicker.valueProperty().addListener(this.cl);
    }

}

public class SpreadSheetComboboxCellType extends SpreadsheetCellType<Color> {
    @Override
    public SpreadsheetCellEditor createEditor(SpreadsheetView spreadsheetView) {
        return new comboboxCellEditor(spreadsheetView);
    }

    @Override
    public String toString(Color color) {
        return color.toString();
    }

    @Override
    public boolean match(Object o) {
        return true;

    }

    @Override
    public Color convertValue(Object o) {
        if (o instanceof Color)
            return (Color) o;
        else {
            return Color.valueOf((String) o);
        }
    }

    public SpreadsheetCell createCell(int row, int column, int rowSpan, int columnSpan, Color value) {
        SpreadsheetCellBase cell = new SpreadsheetCellBase(row, column, rowSpan, columnSpan, this);
        cell.setItem(value);
        Label label = new Label();
        label.setGraphic(null);
        label.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        cell.setGraphic(label);

        return cell;
    }

}

每次修改项目时,SpreadsheetCell 都有一个 updateText 方法。因此它意味着首先显示文本。

您有多种选择。

最简单的方法是向单元格添加一个侦听器并在此时修改您的图形: 单元格中显示的文本将是您的 SpreadsheetCellType 转换的结果。由于您不需要文本,return 您的 SpreadSheetComboboxCellType 中没有任何内容:

@Override
public String toString(Color color) {
    return "";
}

然后只需在您的 SpreadsheetCell 上添加一个监听器并修改图形即可:

cell.itemProperty().addListener(new ChangeListener<Object>() {
        @Override
        public void changed(ObservableValue<? extends Object> ov, Object t, Object newValue) {

            if (newValue != null && newValue instanceof Color ) {
                //Do something
                cell.setGraphic(My_Graphic_Object);
            } else {
                cell.setGraphic(null);
            }
        }
    });

这应该可以解决问题。现在您可以实现自己的 SpreadsheetCell 以完全控制显示。它会更有效率。基本上复制 SpreadsheetCellBase 的所有代码(此处:https://bitbucket.org/controlsfx/controlsfx/src/755c59605e623476ff0a6c860e2c218488776aec/controlsfx/src/main/java/org/controlsfx/control/spreadsheet/SpreadsheetCellBase.java?at=default&fileviewer=file-view-default) 并简单地修改 updateText 方法:

 private void updateText() {
    if(getItem() == null){
        text.setValue(""); //$NON-NLS-1$
    }else if (!("").equals(getFormat())) { //$NON-NLS-1$
        text.setValue(type.toString(getItem(), getFormat()));
    } else {
        text.setValue(type.toString(getItem()));
    }
}

您将修改具有新颜色的图形,而不是修改文本。