【问题标题】:JavaFX - Change ListView's FocusModelJavaFX - 更改 ListView 的 FocusModel
【发布时间】:2020-11-03 21:51:26
【问题描述】:

我有一个 ListView,目前正在覆盖 SelectionModel 以防止选择项目,但是当您尝试选择一个项目时,它仍然显示轮廓。

阅读 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?
  • @CemIkta 我正在使用 ListView 在自己的部分(VBox)中显示“设置”页面中的项目。我希望项目本身可用(当然),但我不希望用户能够选择各个 VBox 部分。
  • 在设置对话框中简单地更改选定/聚焦单元格的样式可能是一种更好的方法。
  • @Slaw 你是对的,谢谢。我最初选择这条路线是因为我曾看过您以前的一个 cmets 关于 ListView.focusModel 的内容,您在其中做了同样的事情。
  • @piggy 我记得在某处做过这样的回答或评论。但是,我不记得您在这个问题中遇到的问题。无论如何,我相信改变风格是更容易和更合适的解决方案。

标签: java eclipse listview javafx


【解决方案1】:

您可以更改您的 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;
}

【讨论】:

  • 感谢您的回复。对于我的具体情况,我最终不得不做一些不同的事情,我也发布了我的答案。
【解决方案2】:

对于我的具体情况,我最终做了以下事情:

listView.setSelectionModel(new SettingsSelectionModel<VBox>());
listView.getStyleClass().add("settings-view");

SettingsSelectionModel 遵循以下答案:https://stackoverflow.com/a/46186195/7987450

我的 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;
}

我每次都选择将背景颜色设置为 null,因为这适用于深色和浅色模式。

【讨论】:

    猜你喜欢
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    相关资源
    最近更新 更多