this is the tableCell variant of JDK-8145588 - isn't it supposed to be fixed for all descendants of Cell (where it was fixed)?
Anyway, to reproduce, run the example below as-is and select a cell
- expected: cell content is prefixed by "winner!!)
- actual: cell content unchanged
the reason seems to be the same as in the referenced old bug - at least it can be hacked-around in a similar way: override updateSelected and call updateItem manually (uncomment the method in the example below)
Please make sure to fix it for _all_ cell types ;)
the example:
public class TableCellContentOnSelection extends Application {
private Parent createContent() {
ObservableList<Locale> data = FXCollections.observableArrayList(
Arrays.stream(Locale.getAvailableLocales(), 10, 20).collect(Collectors.toList()));
TableView<Locale> table = new TableView<>(data);
table.getSelectionModel().setCellSelectionEnabled(true);
table.getColumns().addAll(createTableColumn("displayLanguage"));
return new BorderPane(table);
}
private <T> TableColumn<T, String> createTableColumn(String property) {
TableColumn<T, String> column = new TableColumn<>(property);
column.setCellValueFactory(new PropertyValueFactory<>(property));
column.setCellFactory(cc -> {
// custom cell with text depending on selection state
TableCell<T, String> cell = new TableCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText("");
} else {
if (isSelected()) {
item = "winner!! " + item;
}
setText(item);
}
}
/**
* Hack around https://bugs.openjdk.java.net/browse/JDK-8145588
* variant for TableCell
* @param selected
*/
// @Override
// public void updateSelected(boolean selected) {
// super.updateSelected(selected);
// updateItem(getItem(), isEmpty());
// }
};
return cell;
});
return column;
}
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent()));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
@SuppressWarnings("unused")
private static final Logger LOG = Logger
.getLogger(TableCellContentOnSelection.class.getName());
}
Anyway, to reproduce, run the example below as-is and select a cell
- expected: cell content is prefixed by "winner!!)
- actual: cell content unchanged
the reason seems to be the same as in the referenced old bug - at least it can be hacked-around in a similar way: override updateSelected and call updateItem manually (uncomment the method in the example below)
Please make sure to fix it for _all_ cell types ;)
the example:
public class TableCellContentOnSelection extends Application {
private Parent createContent() {
ObservableList<Locale> data = FXCollections.observableArrayList(
Arrays.stream(Locale.getAvailableLocales(), 10, 20).collect(Collectors.toList()));
TableView<Locale> table = new TableView<>(data);
table.getSelectionModel().setCellSelectionEnabled(true);
table.getColumns().addAll(createTableColumn("displayLanguage"));
return new BorderPane(table);
}
private <T> TableColumn<T, String> createTableColumn(String property) {
TableColumn<T, String> column = new TableColumn<>(property);
column.setCellValueFactory(new PropertyValueFactory<>(property));
column.setCellFactory(cc -> {
// custom cell with text depending on selection state
TableCell<T, String> cell = new TableCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText("");
} else {
if (isSelected()) {
item = "winner!! " + item;
}
setText(item);
}
}
/**
* Hack around https://bugs.openjdk.java.net/browse/JDK-8145588
* variant for TableCell
* @param selected
*/
// @Override
// public void updateSelected(boolean selected) {
// super.updateSelected(selected);
// updateItem(getItem(), isEmpty());
// }
};
return cell;
});
return column;
}
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent()));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
@SuppressWarnings("unused")
private static final Logger LOG = Logger
.getLogger(TableCellContentOnSelection.class.getName());
}
- relates to
-
JDK-8145588 Custom rendering for selected ComboBox list item fails
- Resolved