JavaFX - 更改 ListView 的 FocusModel
JavaFX - Change ListView's FocusModel
我有一个 ListView,目前正在覆盖 SelectionModel 以防止 select 项目的离子,但是当您尝试 select 一个项目时,它仍然显示轮廓。
阅读 API,我发现我可以做同样的事情,但这次是通过使用
覆盖 FocusModel
listView.setFocusModel(new SettingsFocusModel<VBox>());
这是我的 SettingsFocusModel
public class SettingsFocusModel<T> extends FocusModel<T> {
@Override
protected int getItemCount() {
return 0;
}
@Override
protected T getModelItem(int index) {
return null;
}
}
不过,它没有按预期工作。我仍然可以看到轮廓,就好像我没有覆盖 FocusModel 一样。感谢您的帮助!
您可以更改您的 ListView CSS 来防止这种情况:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class HelloFX extends Application {
@Override
public void start(Stage stage) {
ListView<String> listView = new ListView<>();
listView.getStyleClass().add("list-view-unselect");
listView.getItems().add("Item 1");
listView.getItems().add("Item 2");
listView.getItems().add("Item 3");
VBox vbox = new VBox(listView);
Scene scene = new Scene(vbox, 640, 480);
scene.getStylesheets().add("style.css");
stage.setScene(scene);
stage.setTitle("JavaFX App");
stage.show();
}
public static void main(String[] args) {
launch();
}
}
和style.css:
.list-view-unselect .list-cell:filled:selected:focused,
.list-view-unselect .list-cell:filled:selected {
-fx-background-color: white;
-fx-text-fill: black;
}
.list-view-unselect .list-cell:even {
-fx-background-color: white;
-fx-text-fill: black;
}
.list-view-unselect .list-cell:odd {
-fx-background-color: white;
-fx-text-fill: black;
}
对于我的具体情况,我最终执行了以下操作:
listView.setSelectionModel(new SettingsSelectionModel<VBox>());
listView.getStyleClass().add("settings-view");
SettingsSelectionModel 遵循以下答案:
我的 css 文件包括:
.list-view:focused > .virtual-flow > .clipped-container > .sheet > .list-cell:focused {
-fx-background-color: null;
}
.list-view:focused > .virtual-flow > .clipped-container > .sheet > .list-cell:hover {
-fx-background-color: null;
}
.list-view:focused > .virtual-flow > .clipped-container > .sheet > .list-cell:filled:selected {
-fx-background-color: null;
}
我每次都选择将背景颜色设置为空,因为这在深色和浅色模式下都有效。
我有一个 ListView,目前正在覆盖 SelectionModel 以防止 select 项目的离子,但是当您尝试 select 一个项目时,它仍然显示轮廓。
阅读 API,我发现我可以做同样的事情,但这次是通过使用
覆盖 FocusModel listView.setFocusModel(new SettingsFocusModel<VBox>());
这是我的 SettingsFocusModel
public class SettingsFocusModel<T> extends FocusModel<T> {
@Override
protected int getItemCount() {
return 0;
}
@Override
protected T getModelItem(int index) {
return null;
}
}
不过,它没有按预期工作。我仍然可以看到轮廓,就好像我没有覆盖 FocusModel 一样。感谢您的帮助!
您可以更改您的 ListView CSS 来防止这种情况:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class HelloFX extends Application {
@Override
public void start(Stage stage) {
ListView<String> listView = new ListView<>();
listView.getStyleClass().add("list-view-unselect");
listView.getItems().add("Item 1");
listView.getItems().add("Item 2");
listView.getItems().add("Item 3");
VBox vbox = new VBox(listView);
Scene scene = new Scene(vbox, 640, 480);
scene.getStylesheets().add("style.css");
stage.setScene(scene);
stage.setTitle("JavaFX App");
stage.show();
}
public static void main(String[] args) {
launch();
}
}
和style.css:
.list-view-unselect .list-cell:filled:selected:focused,
.list-view-unselect .list-cell:filled:selected {
-fx-background-color: white;
-fx-text-fill: black;
}
.list-view-unselect .list-cell:even {
-fx-background-color: white;
-fx-text-fill: black;
}
.list-view-unselect .list-cell:odd {
-fx-background-color: white;
-fx-text-fill: black;
}
对于我的具体情况,我最终执行了以下操作:
listView.setSelectionModel(new SettingsSelectionModel<VBox>());
listView.getStyleClass().add("settings-view");
SettingsSelectionModel 遵循以下答案:
我的 css 文件包括:
.list-view:focused > .virtual-flow > .clipped-container > .sheet > .list-cell:focused {
-fx-background-color: null;
}
.list-view:focused > .virtual-flow > .clipped-container > .sheet > .list-cell:hover {
-fx-background-color: null;
}
.list-view:focused > .virtual-flow > .clipped-container > .sheet > .list-cell:filled:selected {
-fx-background-color: null;
}
我每次都选择将背景颜色设置为空,因为这在深色和浅色模式下都有效。