我已经可以让列表中的多个项目移动到另一个列表但是当我将其移动到另一个列表[]时,这些项目会弹出刚刚移动的项目,并且所选择的多个选项都在一个列表中在列表视图控件中的行,而不是它是列表中垂直向下的单独列出的项目。我需要这个用于作业。这就是我所拥有的。
package com.company.javaFxPractice;
import javafx.collections.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.text.Text;
import javafx.stage.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.application.*;
public class ListProblem extends Application
{
ObservableList<String> girlNames;
ObservableList<String> boyNames;
Button moveLeftButton;
Button moveRightButton;
Scene scene1;
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("List Problem");
primaryStage.setResizable(false);
primaryStage.sizeToScene();
GridPane gridPane = new GridPane();
scene1 = new Scene(gridPane,350,200);
ListView<String> data = new ListView<String>();
data.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
girlNames = FXCollections.observableArrayList(
"Meagan Good", "Jessica Alba", "Erykah Badu", "Beyonce", "Janet"
);
data.setItems(girlNames);
ListView<String> data2 = new ListView<String>();
boyNames = FXCollections.observableArrayList(
"Eddie Murphy", "Richord Pryor", "Eddie Griffin", "Kevin Heart", "Mike"
);
data2.setItems(boyNames);
gridPane.setConstraints(data, 0, 0);
gridPane.setConstraints(data2, 1, 0);
moveLeftButton = new Button("Move Left");
moveLeftButton.setPrefSize(200, 30);
moveLeftButton.setAlignment(Pos.CENTER);
moveLeftButton.setOnAction(event -> {
String selection = "";
//turns selected item on left list control to a string
selection = String.valueOf(data.getSelectionModel().getSelectedItems());
//adds selected Item to the list on the right
boyNames.add(selection);
// removes whatever was selected from the left list
girlNames.remove(data.getSelectionModel().getSelectedItem());
});
gridPane.setConstraints(moveLeftButton, 0, 1);
moveRightButton = new Button("Move Right");
moveRightButton.setPrefSize(200, 30);
moveRightButton.setAlignment(Pos.CENTER);
moveRightButton.setOnAction(event -> {
String selection = "";
//turns selected item on the right list control to a string
selection =
String.valueOf(data2.getSelectionModel().getSelectedItems());
//adds the string to the list on the left
girlNames.add(selection);
// removes whatever was just selected from right list
boyNames.remove(data2.getSelectionModel().getSelectedItem());
});
gridPane.setConstraints(moveRightButton,1,1);
gridPane.getChildren().
addAll(data,data2,moveLeftButton,moveRightButton);
primaryStage.setScene(scene1);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
您无需将selected items
转换为String
。您可以(并且应该)将它们直接添加到其他列表中。
尝试使用:
moveLeftButton.setOnAction(event -> {
boyNames.addAll(data.getSelectionModel().getSelectedItems());
girlNames.removeAll(data.getSelectionModel().getSelectedItems());
});
请注意我使用的是getSelectedItems()
,因为此ListView
已启用 MULTIPLE 选项。
同样地,
moveRightButton.setOnAction(event -> {
girlNames.addAll(data2.getSelectionModel().getSelectedItem());
boyNames.remove(data2.getSelectionModel().getSelectedItem());
});
完成工作示例
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class ListProblem extends Application {
ObservableList<String> girlNames;
ObservableList<String> boyNames;
Button moveLeftButton;
Button moveRightButton;
Scene scene1;
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("List Problem");
primaryStage.setResizable(false);
primaryStage.sizeToScene();
GridPane gridPane = new GridPane();
scene1 = new Scene(gridPane, 350, 200);
ListView<String> data = new ListView<String>();
data.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
girlNames = FXCollections.observableArrayList("Meagan Good",
"Jessica Alba", "Erykah Badu", "Beyonce", "Janet");
data.setItems(girlNames);
ListView<String> data2 = new ListView<String>();
boyNames = FXCollections.observableArrayList("Eddie Murphy",
"Richord Pryor", "Eddie Griffin", "Kevin Heart", "Mike");
data2.setItems(boyNames);
GridPane.setConstraints(data, 0, 0);
GridPane.setConstraints(data2, 1, 0);
moveLeftButton = new Button("Move Left");
moveLeftButton.setPrefSize(200, 30);
moveLeftButton.setAlignment(Pos.CENTER);
moveLeftButton.setOnAction(event -> {
boyNames.addAll(data.getSelectionModel().getSelectedItems());
girlNames.removeAll(data.getSelectionModel().getSelectedItems());
});
GridPane.setConstraints(moveLeftButton, 0, 1);
moveRightButton = new Button("Move Right");
moveRightButton.setPrefSize(200, 30);
moveRightButton.setAlignment(Pos.CENTER);
moveRightButton.setOnAction(event -> {
girlNames.addAll(data2.getSelectionModel().getSelectedItem());
boyNames.remove(data2.getSelectionModel().getSelectedItem());
});
GridPane.setConstraints(moveRightButton, 1, 1);
gridPane.getChildren().addAll(data, data2, moveLeftButton,
moveRightButton);
primaryStage.setScene(scene1);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}