gpt4 book ai didi

listview - JavaFX ListView 多选

转载 作者:行者123 更新时间:2023-12-04 10:18:09 40 4
gpt4 key购买 nike

我想从 ListView 中选择多个项目。它会在鼠标点击时使用react。我试过这个:

selectedLogsList.addAll(logsListView.getSelectionModel().getSelectedItems());

但它给了我重复。
所以我试过这个:
logsListView.getSelectionModel().selectedItemProperty().addListener(
new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov,
String old_val, String new_val) {
if(selectedLogsList.contains(new_val)) {
selectedLogsList.remove(new_val);
} else {
selectedLogsList.add(new_val);
}
}
});

我也想,如果单元格被选中,它应该被标记为这样(不同的背景颜色)
谢谢!

最佳答案

如果您只想知道选择了哪些项目,请查看我的示例。看看 setOnMouseClicked() 方法..

    package application;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Pane root = new Pane();
Scene scene = new Scene(root,600,600);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

ListView<String> listView = new ListView<String>();

ObservableList<String> list = FXCollections.observableArrayList();





listView.setItems(list);

list.add("item1");
list.add("item2");
list.add("item3");

listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);


listView.setOnMouseClicked(new EventHandler<Event>() {

@Override
public void handle(Event event) {
ObservableList<String> selectedItems = listView.getSelectionModel().getSelectedItems();

for(String s : selectedItems){
System.out.println("selected item " + s);
}

}

});


list.add("item4");

root.getChildren().add(listView);

primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

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

关于listview - JavaFX ListView 多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24158394/

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