Итак, подумав, я могу использовать простую версию для демонстрации, и она должна вписаться в вашу более сложную версию. Ключ создает слушателя для каждого ChoiceBox
. При изменении выбора ChoiceBox
обновите предикат FilteredList
.
Код, который вам нужен
cbBranch.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) ->{
System.out.println("Branch: " + newValue);
filteredData.setPredicate((t) -> {
switch(cbGenre.getValue())
{
case "All":
switch(newValue)
{
case "All":
return true;
default:
return newValue.equals(t.getBranch());
}
default:
return newValue.equals(t.getBranch()) && cbGenre.getValue().equals(t.getGenre());
}
});
});
cbGenre.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue)->{
System.out.println("Genre: " + newValue);
filteredData.setPredicate((t) -> {
switch(cbBranch.getValue())
{
case "All":
switch(newValue)
{
case "All":
return true;
default:
return newValue.equals(t.getGenre());
}
default:
return newValue.equals(t.getGenre()) && cbGenre.getValue().equals(t.getBranch());
}
});
});
Полный пример
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author Sedrick
*/
public class JavaFXApplication38 extends Application {
@Override
public void start(Stage primaryStage) {
ChoiceBox<String> cbGenre = new ChoiceBox();
cbGenre.getItems().addAll("All", "Horror", "Action");
cbGenre.setValue("All");
ChoiceBox<String> cbBranch = new ChoiceBox();
cbBranch.getItems().addAll("All", "Branch1", "Branch2");
cbBranch.setValue("All");
ObservableList<Movie> movieList = FXCollections.observableArrayList();
movieList.add(new Movie("Horror", "IT", "Branch1", "Released", "Warner Bros"));
movieList.add(new Movie("Action","John Wick 3" ,"Branch2", "Coming Soon", "Summit Entertainment"));
FilteredList<Movie> filteredData = new FilteredList<>(movieList, s -> true);
ListView<Movie> listView = new ListView<>(filteredData);
listView.setCellFactory((ListView<Movie> param) -> {
ListCell<Movie> cell = new ListCell<Movie>() {
@Override
protected void updateItem(Movie item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getTitle());
} else {
setText("");
}
}
};
return cell;
});
cbBranch.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) ->{
System.out.println("Branch: " + newValue);
filteredData.setPredicate((t) -> {
switch(cbGenre.getValue())
{
case "All":
switch(newValue)
{
case "All":
return true;
default:
return newValue.equals(t.getBranch());
}
default:
return newValue.equals(t.getBranch()) && cbGenre.getValue().equals(t.getGenre());
}
});
});
cbGenre.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue)->{
System.out.println("Genre: " + newValue);
filteredData.setPredicate((t) -> {
switch(cbBranch.getValue())
{
case "All":
switch(newValue)
{
case "All":
return true;
default:
return newValue.equals(t.getGenre());
}
default:
return newValue.equals(t.getGenre()) && cbGenre.getValue().equals(t.getBranch());
}
});
});
HBox root = new HBox(new VBox(cbBranch, cbGenre), listView);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Обновление! Код теперь имеет Predicate
лучше для обработки ChoiceBoxes
.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author Sedrick
*/
public class JavaFXApplication38 extends Application {
@Override
public void start(Stage primaryStage) {
ChoiceBox<String> cbGenre = new ChoiceBox();
cbGenre.getItems().addAll("All", "Horror", "Action");
cbGenre.setValue("All");
ChoiceBox<String> cbBranch = new ChoiceBox();
cbBranch.getItems().addAll("All", "Branch1", "Branch2");
cbBranch.setValue("All");
ChoiceBox<String> cbRelease = new ChoiceBox();
cbRelease.getItems().addAll("All", "Released", "Coming Soon");
cbRelease.setValue("All");
ChoiceBox<String> cbParentCompany = new ChoiceBox();
cbParentCompany.getItems().addAll("All", "Warner Bros", "Summit Entertainment");
cbParentCompany.setValue("All");
ChoiceBox<String> cbTitle = new ChoiceBox();
cbTitle.getItems().addAll("All", "IT", "John Wick 3");
cbTitle.setValue("All");
ObservableList<Movie> movieList = FXCollections.observableArrayList();
movieList.add(new Movie("Horror", "IT", "Branch1", "Released", "Warner Bros"));
movieList.add(new Movie("Action","John Wick 3" ,"Branch2", "Coming Soon", "Summit Entertainment"));
FilteredList<Movie> filteredData = new FilteredList<>(movieList, s -> true);
ListView<Movie> listView = new ListView<>(filteredData);
listView.setCellFactory((ListView<Movie> param) -> {
ListCell<Movie> cell = new ListCell<Movie>() {
@Override
protected void updateItem(Movie item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getTitle());
} else {
setText("");
}
}
};
return cell;
});
cbRelease.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) ->{
System.out.println("Released: " + newValue);
filteredData.setPredicate((t) -> {
return (cbBranch.getValue().equals("All") ? true : t.getBranch().equals(cbBranch.getValue())) &&
(cbGenre.getValue().equals("All") ? true : t.getGenre().equals(cbGenre.getValue())) &&
(cbParentCompany.getValue().equals("All") ? true : t.getParentCompany().equals(cbParentCompany.getValue())) &&
(cbTitle.getValue().equals("All") ? true : t.getTitle().equals(cbTitle.getValue())) &&
(cbRelease.getValue().equals("All") ? true : t.getRelease().equals(cbRelease.getValue()));
});
});
cbBranch.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) ->{
System.out.println("Branch: " + newValue);
filteredData.setPredicate((t) -> {
return (cbBranch.getValue().equals("All") ? true : t.getBranch().equals(newValue)) &&
(cbGenre.getValue().equals("All") ? true : t.getGenre().equals(cbGenre.getValue())) &&
(cbParentCompany.getValue().equals("All") ? true : t.getParentCompany().equals(cbParentCompany.getValue())) &&
(cbTitle.getValue().equals("All") ? true : t.getTitle().equals(cbTitle.getValue())) &&
(cbRelease.getValue().equals("All") ? true : t.getRelease().equals(cbRelease.getValue()));
});
});
cbGenre.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue)->{
System.out.println("Genre: " + newValue);
filteredData.setPredicate((t) -> {
return (cbBranch.getValue().equals("All") ? true : t.getBranch().equals(cbBranch.getValue())) &&
(cbGenre.getValue().equals("All") ? true : t.getGenre().equals(cbGenre.getValue())) &&
(cbParentCompany.getValue().equals("All") ? true : t.getParentCompany().equals(cbParentCompany.getValue())) &&
(cbTitle.getValue().equals("All") ? true : t.getTitle().equals(cbTitle.getValue())) &&
(cbRelease.getValue().equals("All") ? true : t.getRelease().equals(cbRelease.getValue()));
});
});
cbParentCompany.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue)->{
System.out.println("parent company: " + newValue);
filteredData.setPredicate((t) -> {
return (cbBranch.getValue().equals("All") ? true : t.getBranch().equals(cbBranch.getValue())) &&
(cbGenre.getValue().equals("All") ? true : t.getGenre().equals(cbGenre.getValue())) &&
(cbParentCompany.getValue().equals("All") ? true : t.getParentCompany().equals(cbParentCompany.getValue())) &&
(cbTitle.getValue().equals("All") ? true : t.getTitle().equals(cbTitle.getValue())) &&
(cbRelease.getValue().equals("All") ? true : t.getRelease().equals(cbRelease.getValue()));
});
});
cbTitle.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue)->{
System.out.println("title: " + newValue);
filteredData.setPredicate((t) -> {
return (cbBranch.getValue().equals("All") ? true : t.getBranch().equals(cbBranch.getValue())) &&
(cbGenre.getValue().equals("All") ? true : t.getGenre().equals(cbGenre.getValue())) &&
(cbParentCompany.getValue().equals("All") ? true : t.getParentCompany().equals(cbParentCompany.getValue())) &&
(cbTitle.getValue().equals("All") ? true : t.getTitle().equals(cbTitle.getValue())) &&
(cbRelease.getValue().equals("All") ? true : t.getRelease().equals(cbRelease.getValue()));
});
});
HBox root = new HBox(new VBox(cbBranch, cbGenre, cbRelease, cbParentCompany, cbTitle), listView);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}