Regarding your note:
Note: the same fx-css can be applied to a table-view and this unwanted functionally does not occur.
I assume you're applying the style to the table rows and not the table cells. I'm making that assumption for a couple reasons:
- Table cells, at least by default, don't seem to have a background. This means the drop shadow was applied to the text of the cell in my tests rather than the entire cell.
- It's only
TableRowSkinBase
that does something different to ListCellSkin
that's potentially relevant to this problem.
That difference mentioned in the second point is a call to setPickOnBounds(false)
. That isn't called for ListCell
. Apparently the drop shadow increases the bounds of the cell causing it to overlap its adjacent cells and, since pick-on-bounds is true, that means the mouse hovers over the cell for longer than "expected".
Simply calling setPickOnBounds(false)
on your custom list cells should fix the issue. For example:
Main.java:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
var listView = new ListView<String>();
listView.getItems().addAll("1", "2", "3", "4", "5");
listView.setCellFactory(
lv ->
new ListCell<>() {
{
setPrefHeight(38);
setPickOnBounds(false); // fix for issue
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
setText(item);
}
}
});
var scene = new Scene(listView, 600, 400);
scene.getStylesheets().add(getClass().getResource("/test.css").toString());
primaryStage.setScene(scene);
primaryStage.show();
}
}
test.css:
.list-cell:selected:filled:hover {
-fx-view-order: -1;
-fx-effect: dropshadow(gaussian, #67676D, 12, 0.05, 0.0, 2);
}
Note: I do not know if calling setPickOnBounds(false)
causes other issues or not.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…