[英]ListView - removeAll doesn't work?
(This is code from the book "JavaFX 2.0 by example" by Carl Dea - the code example is freely available at Apress so I'm sure they don't mind me using it here) (这是来自Carl Dea的“JavaFX 2.0示例”一书中的代码 - 代码示例在Apress免费提供,所以我确定他们不介意我在这里使用它)
I have example code which works perfectly 我有完美的示例代码
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();
}
}
It is code for moving persons back and forth between two listviews, one at a time. 它是用于在两个列表视图之间来回移动的代码,一次一个。 What I want to do is make it possible to select and move several persons in one click.
我想要做的是一键选择和移动几个人。
The relevant excerpts I want to change are: 我想改变的相关摘录是:
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);
}
}
});
What I have tried changing: 我尝试过改变:
First I add the following import: 首先,我添加以下导入:
import javafx.scene.control.SelectionMode;
Then I add the lines 然后我添加线条
candidatesListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
heroListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
beneath the respective declarations of the two lists. 在两个清单的相应声明之下。
Lastly I change the code of the handling of the eventbutton to 最后,我将事件按钮的处理代码更改为
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();
}
}
That is- I change it to getSelectedItem_s_, then I addAll and removeAll instead of merely adding/removing one person. 那就是 - 我将它改为getSelectedItem_s_,然后我addAll和removeAll而不是仅添加/删除一个人。 This just leaves the listView blank when I try to move several people over.
当我尝试移动几个人时,这只会将listView留空。 What gives?
是什么赋予了?
Ps. PS。 I also tried just adding/removing several people one at a time by iterating over the list "potential", but that also gave the wrong result.
我还尝试通过迭代“潜在”列表一次一个地添加/删除几个人,但这也给出了错误的结果。
Unfortunately you've met a bug: http://javafx-jira.kenai.com/browse/RT-24367 不幸的是你遇到了一个错误: http : //javafx-jira.kenai.com/browse/RT-24367
The original problem is next: ListView.getSelectionMode()
returns part of it's observable list but not the copy. 接下来是原始问题:
ListView.getSelectionMode()
返回其可观察列表的一部分,但不返回副本。 So removing from that list leads to various issues. 因此,从该列表中删除会导致各种问题。
Use next code which copies list before removing items from it: 在从中删除项目之前,请使用下一个复制列表的代码:
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();
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.