gpt4 book ai didi

带有自定义单元格工厂的 javafx ListView 不保留所选单元格

转载 作者:行者123 更新时间:2023-12-02 06:39:03 24 4
gpt4 key购买 nike

我有一个controlsfx CheckListView(甚至与javafx ListView控件有同样的问题),我想在其中显示RadioButtons而不是CheckBox。因此,我通过一些 javafx 教程的帮助实现了自定义单元工厂,并且它正在工作。问题是我选择了第一个单选按钮并向下滚动一点,这样我的顶部单选按钮中的几个就会向上滚动并且现在不可见。然后我再次向上滚动,选择现在消失了。

我调试代码后了解到每次都会创建新单元格,这会导致此问题,但不幸的是无法找出解决方案。

我附上了一个示例代码,该代码是我从堆栈溢出中获得的,它也有同样的问题。

package application;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class RadioButtonListView extends Application {

public static final ObservableList names = FXCollections.observableArrayList();
private ToggleGroup group = new ToggleGroup();

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("List View Sample");

final ListView listView = new ListView();
listView.setPrefSize(200, 250);
listView.setEditable(true);

names.addAll("Adam", "Alex", "Alfred", "Albert", "Brenda", "Connie", "Derek", "Donny", "Lynne", "Myrtle", "Rose", "Rudolph", "Tony", "Trudy", "Williams", "Zach");

listView.setItems(names);
listView.setCellFactory(param -> new RadioListCell());
StackPane root = new StackPane();
root.getChildren().add(listView);
primaryStage.setScene(new Scene(root, 200, 250));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}

private class RadioListCell extends ListCell<String> {
@Override
public void updateItem(String obj, boolean empty) {
super.updateItem(obj, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
RadioButton radioButton = new RadioButton(obj);
radioButton.setToggleGroup(group);
// Add Listeners if any
setGraphic(radioButton);
}
}
}
}

请在这方面需要您的帮助。(我正在使用 javafx 8)

最佳答案

您应该为单元格创建一个单选按钮(而不是每次调用 updateItem(...) 时创建一个新单选按钮,并在 updateItem( ...) 方法使用数据表示中的适当逻辑。

private class RadioListCell extends ListCell<String> {

private final RadioButton radioButton = new RadioButton();

RadioListCell() {
radioButton.setToggleGroup(group);
// Add listeners here...
}

@Override
public void updateItem(String obj, boolean empty) {
super.updateItem(obj, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
radioButton.setText(obj);

radioButton.setSelected(...);

setGraphic(radioButton);
}
}
}

例如:

import java.util.Objects;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class RadioButtonListView extends Application {

public static final ObservableList<String> names = FXCollections.observableArrayList();
private ToggleGroup group = new ToggleGroup();

private String selectedName ;

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("List View Sample");

final ListView<String> listView = new ListView<>();
listView.setPrefSize(200, 250);
listView.setEditable(true);

names.addAll("Adam", "Alex", "Alfred", "Albert", "Brenda", "Connie", "Derek", "Donny", "Lynne", "Myrtle", "Rose", "Rudolph", "Tony", "Trudy", "Williams", "Zach");

listView.setItems(names);
listView.setCellFactory(param -> new RadioListCell());
StackPane root = new StackPane();
root.getChildren().add(listView);
primaryStage.setScene(new Scene(root, 200, 250));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}

private class RadioListCell extends ListCell<String> {

private final RadioButton radioButton = new RadioButton();

RadioListCell() {
radioButton.setToggleGroup(group);
radioButton.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
selectedName = getItem();
}
});
}

@Override
public void updateItem(String obj, boolean empty) {
super.updateItem(obj, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
radioButton.setText(obj);

radioButton.setSelected(Objects.equals(obj, selectedName));

setGraphic(radioButton);
}
}
}
}

关于带有自定义单元格工厂的 javafx ListView 不保留所选单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40507262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com