我继承了一些代码,这些代码将根据检测到的ComboBox值更改来更改ComboBox的边框颜色,以进行扩展。但是,似乎对ComboBox样式所做的任何更改都不会使其在ChangeListener lambda之外。
基本上,ComboBox边框被初始化为黑色,并且我想在用户更改值后更改为颜色,但是ChangeListener内部设置的颜色未应用。当我单步执行代码时,我看到lambda ChangeListener中的样式字符串发生了变化,但是当我退出lambda ChangeListener时,它又回到了初始样式字符串。
我在下面包括了我要修改的代码,以及我要添加的代码,用于插入和调出ComboBox样式。任何帮助将不胜感激。
public class SearchScreenController
{
@FXML
private TableView<ConfigBaselineFileArtifact> searchRecordsTable;
@FXML
private TableColumn<ConfigBaselineFileArtifact, String> categoryColumn = new TableColumn<>("File Category");
@FXML
private TableColumn<ConfigBaselineFileArtifact, ConfigBaselineFileArtifact> subcategoryColumn = new TableColumn<>("File Subcategory")
private void createSearchTable()
{
searchRecordsTable.getColumns().clear();
//File Category and Subcategory Columns
categoryColumn.setCellValueFactory(new PropertyValueFactory<>("fileCategoryName"));
subcategoryColumn.setCellValueFactory(value -> new SimpleObjectProperty(value.getValue()));
categoryColumn.setCellFactory(param -> new TableCell<ConfigBaselineFileArtifact, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
ComboBox<String> cb = new ComboBox<String>(FXCollections.observableArrayList(categoryList));
cb.setValue(item);
cb.setPrefWidth(145);
cb.setStyle("-fx-background-color: transparent; -fx-border-color: black;");
cb.getSelectionModel().selectedItemProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
if (newValue != null) {
int index = getIndex();
// Test for case that updated combobox isn't listed with list of selected records
if (!searchRecordsTable.getSelectionModel().getSelectedIndices().contains(index)) {
searchRecordsTable.getItems().get(index).setFileCategoryName(newValue);
searchRecordsTable.getItems().get(index).setFileSubCategoryName("");
DataStore.getInstance().saveConfigBaselineFileArtifact(searchRecordsTable.getItems().get(index));
} else {
// Else if updated combobox IS within list of selected records
searchRecordsTable.getSelectionModel().getSelectedItems().forEach(record -> {
record.setFileCategoryName(newValue);
record.setFileSubCategoryName("");
DataStore.getInstance().saveConfigBaselineFileArtifact(record);
});
}
// Code I'm trying to add is below
if (newValue.equals("")) {
cb.setStyle("-fx-background-color: transparent; -fx-border-color: red;");
} else {
cb.setStyle("-fx-background-color: transparent; -fx-border-color: green;");
// Code I'm trying to add is above
searchRecordsTable.refresh();
}
});
setGraphic(cb);
}
}
});
searchRecordsTable.getColumns().addAll(checkBoxColumn, fileNameColumn, artifactNameColumn, categoryColumn, subcategoryColumn, sensorNameColumn, subsystemColumn, componentColumn, dateImportedColumn, importedByColumn, labelsColumn);
searchRecordsTable.refresh();
searchRecordsTable.setEditable(true);
searchRecordsTable.getSelectionModel().getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}
}
答案 0 :(得分:0)
我认为问题是,您的searchRecordsTable.refresh();
通话正在重置边框。似乎updateItem(String item, boolean empty)
函数被再次调用。您需要确保将状态存储在其他任何地方,例如一个HashMap。像这样:
private Map<String, Boolean> changeMap = new HashMap<>();
//in your selectedItem listener
if (newValue.equals("")) {
changeMap.remove(item);
} else {
changeMap.put(item, true);
}
//before setGraphic(cb)
if(changeMap.contains(item)) {
cb.setStyle("-fx-background-color: transparent; -fx-border-color: green;");
}