我创造了一个
MCVE
. 代码中的注释。它显示了如何设置
FilteredList
以及如何将数据从一个
ListView
给另一个。
主要的
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication239 extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
控制器
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TextField;
/**
*
* @author blj0011
*/
public class FXMLDocumentController implements Initializable
{
@FXML
private ListView lvLeft, lvRight;
@FXML
private TextField tfLeft, tfRight;
ObservableList<String> leftData = FXCollections.observableArrayList();
ObservableList<String> rightData = FXCollections.observableArrayList();
FilteredList<String> filteredLeftData, filteredRightData;
@Override
public void initialize(URL url, ResourceBundle rb)
{
leftData.addAll(getFakeDataFromDB());//get data from DB
//rightData.addAll(getFakeDataFromDB());//Used for testing
filteredLeftData = new FilteredList(leftData, s -> true);
filteredRightData = new FilteredList(rightData, s -> true);
//Set filtered Lists
tfLeft.textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null || newValue.length() == 0) {
filteredLeftData.setPredicate(null);
}
else {
filteredLeftData.setPredicate(t -> {
return t.toUpperCase().startsWith(newValue.toUpperCase());
});
}
});
tfRight.textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null || newValue.length() == 0) {
filteredRightData.setPredicate(null);
}
else {
filteredRightData.setPredicate(t -> {
return t.toUpperCase().startsWith(newValue.toUpperCase());
});
}
});
//Set listview items
lvLeft.setItems(filteredLeftData);
lvRight.setItems(filteredRightData);
//Set selecton model selection mode
lvLeft.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
lvRight.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}
@FXML
private void handleBtnActionMoveToLeft(ActionEvent actionEvent)
{
List<String> itemsToMove = new ArrayList(lvRight.getSelectionModel().getSelectedItems());//If you don't do this "new ArrayList(..):", then you need to first addAll then removeAll
System.out.println("list size: " + itemsToMove.size());
rightData.removeAll(itemsToMove);
leftData.addAll(itemsToMove);
}
@FXML
private void handleBtnActionMoveToRight(ActionEvent actionEvent)
{
List<String> itemsToMove = new ArrayList(lvLeft.getSelectionModel().getSelectedItems());//If you don't do this "new ArrayList(..):", then you need to first addAll then removeAll
System.out.println("list size: " + itemsToMove.size());
leftData.removeAll(itemsToMove);
rightData.addAll(itemsToMove);
}
List<String> getFakeDataFromDB()
{
List<String> tempList = new ArrayList();
tempList.add("Hello");
tempList.add("Hello World!");
tempList.add("Bye");
tempList.add("Bye World!");
tempList.add("Been");
tempList.add("Bad");
return tempList;
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication239.FXMLDocumentController">
<children>
<ListView fx:id="lvLeft" prefHeight="200.0" prefWidth="200.0" />
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" HBox.hgrow="ALWAYS">
<children>
<Label text="Left" />
<TextField fx:id="tfLeft" />
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="5.0">
<children>
<Button mnemonicParsing="false" onAction="#handleBtnActionMoveToRight" text="Move To Right" />
<Button mnemonicParsing="false" onAction="#handleBtnActionMoveToLeft" text="Move To Left" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
</VBox>
<Label text="Right" />
<TextField fx:id="tfRight" />
</children>
</VBox>
<ListView fx:id="lvRight" prefHeight="200.0" prefWidth="200.0" />
</children>
</HBox>