gpt4 book ai didi

java - ListView - removeAll 不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:14:25 25 4
gpt4 key购买 nike

(这是来自 Carl Dea 的“JavaFX 2.0 by example”一书中的代码 - 该代码示例可在 Apress 免费获得,所以我相信他们不介意我在这里使用它)

我有完美运行的示例代码

package javafx2introbyexample.chapter1.recipe1_11;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @author cdea
*/
public class CreatingAndWorkingWithObservableLists extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Chapter 1-11 Creating and Working with ObservableLists");
Group root = new Group();
Scene scene = new Scene(root, 400, 250, Color.WHITE);

// create a grid pane
GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(5));
gridpane.setHgap(10);
gridpane.setVgap(10);

// candidates label
Label candidatesLbl = new Label("Candidates");
GridPane.setHalignment(candidatesLbl, HPos.CENTER);
gridpane.add(candidatesLbl, 0, 0);

Label heroesLbl = new Label("Heroes");
gridpane.add(heroesLbl, 2, 0);
GridPane.setHalignment(heroesLbl, HPos.CENTER);

// candidates
final ObservableList<String> candidates = FXCollections.observableArrayList("Superman",
"Spiderman",
"Wolverine",
"Police",
"Fire Rescue",
"Soldiers",
"Dad & Mom",
"Doctor",
"Politician",
"Pastor",
"Teacher");
final ListView<String> candidatesListView = new ListView<String>(candidates);
candidatesListView.setPrefWidth(150);
candidatesListView.setPrefHeight(150);

gridpane.add(candidatesListView, 0, 1);

// heros
final ObservableList<String> heroes = FXCollections.observableArrayList();
final ListView<String> heroListView = new ListView<String>(heroes);
heroListView.setPrefWidth(150);
heroListView.setPrefHeight(150);

gridpane.add(heroListView, 2, 1);


// select heroes
Button sendRightButton = new Button(">");
sendRightButton.setOnAction(new EventHandler<ActionEvent>() {

public void handle(ActionEvent event) {
String potential = candidatesListView.getSelectionModel().getSelectedItem();
if (potential != null) {
candidatesListView.getSelectionModel().clearSelection();
candidates.remove(potential);
heroes.add(potential);
}
}
});

// deselect heroes
Button sendLeftButton = new Button("<");
sendLeftButton.setOnAction(new EventHandler<ActionEvent>() {

public void handle(ActionEvent event) {
String notHero = heroListView.getSelectionModel().getSelectedItem();
if (notHero != null) {
heroListView.getSelectionModel().clearSelection();
heroes.remove(notHero);
candidates.add(notHero);
}
}
});

VBox vbox = new VBox(5);
vbox.getChildren().addAll(sendRightButton,sendLeftButton);

gridpane.add(vbox, 1, 1);
GridPane.setConstraints(vbox, 1, 1, 1, 2,HPos.CENTER, VPos.CENTER);

root.getChildren().add(gridpane);
primaryStage.setScene(scene);
primaryStage.show();
}
}

它是用于在两个 ListView 之间来回移动人员的代码,一次一个。我想做的是让一次点击选择和移动多个人成为可能。

我要更改的相关摘录是:

final ListView<String> candidatesListView = new ListView<String>(candidates);
candidatesListView.setPrefWidth(150);
candidatesListView.setPrefHeight(150);

gridpane.add(candidatesListView, 0, 1);

// heros
final ObservableList<String> heroes = FXCollections.observableArrayList();
final ListView<String> heroListView = new ListView<String>(heroes);

...

// select heroes
Button sendRightButton = new Button(">");
sendRightButton.setOnAction(new EventHandler<ActionEvent>() {

public void handle(ActionEvent event) {
String potential = candidatesListView.getSelectionModel().getSelectedItem();
if (potential != null) {
candidatesListView.getSelectionModel().clearSelection();
candidates.remove(potential);
heroes.add(potential);
}
}
});

// deselect heroes
Button sendLeftButton = new Button("<");
sendLeftButton.setOnAction(new EventHandler<ActionEvent>() {

public void handle(ActionEvent event) {
String notHero = heroListView.getSelectionModel().getSelectedItem();
if (notHero != null) {
heroListView.getSelectionModel().clearSelection();
heroes.remove(notHero);
candidates.add(notHero);
}
}
});

我尝试改变的是:

首先我添加以下导入:

import javafx.scene.control.SelectionMode;

然后我添加行

candidatesListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
heroListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

在两个列表各自的声明下方。

最后我将事件按钮的处理代码更改为

public void handle(ActionEvent event) {
ObservableList<String> potential = candidatesListView.getSelectionModel().getSelectedItems();
if (potential != null) {

System.out.println(potential);
candidates.removeAll(potential);
heroes.addAll(potential);
candidatesListView.getSelectionModel().clearSelection();
}
}

那是——我将其更改为 getSelectedItem_s_,然后我添加所有和删除所有,而不是仅仅添加/删除一个人。当我尝试移动几个人时,这只会让 listView 空白。给了什么?

附言。我还尝试通过遍历“潜在”列表一次只添加/删除几个人,但这也给出了错误的结果。

最佳答案

很遗憾,您遇到了一个错误:http://javafx-jira.kenai.com/browse/RT-24367

接下来是原始问题:ListView.getSelectionMode() 返回其可观察列表的一部分,但不返回副本。因此从该列表中删除会导致各种问题。

使用下一个代码在从列表中删除项目之前复制列表:

    sendRightButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
ObservableList<String> potential =
FXCollections.observableArrayList( //copy
candidatesListView.getSelectionModel().getSelectedItems());
if (potential != null) {
heroes.addAll(potential);
candidates.removeAll(potential);
candidatesListView.getSelectionModel().clearSelection();
}
}
});

关于java - ListView - removeAll 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12132896/

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