gpt4 book ai didi

java - ControlsFX 电子表格 View : How to set SpreadsheetCell to graphic only?

转载 作者:行者123 更新时间:2023-12-01 09:46:04 26 4
gpt4 key购买 nike

我已经创建了自己的 SpreadsheetCellEditor 来显示 ColorPicker,但我不想返回 ColorPicker.getValue().toString,我想返回一个标签所选值的背景颜色。我已经搜索了 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 转换的结果。由于您不需要文本,因此在 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()));
}
}

您将使用新颜色修改图形,而不是修改文本。

关于java - ControlsFX 电子表格 View : How to set SpreadsheetCell to graphic only?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38021407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com