Skip to content

Instantly share code, notes, and snippets.

@aaronanderson
Last active September 17, 2016 15:23
Show Gist options
  • Save aaronanderson/96e9cf4905cf48ad4510828fb3c1113b to your computer and use it in GitHub Desktop.
Save aaronanderson/96e9cf4905cf48ad4510828fb3c1113b to your computer and use it in GitHub Desktop.
MultiList - Java FX SelectListView
package com.mercer.cpsg.workspace.app.uicomponents;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXButton.ButtonType;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
public class MultiList<T> extends VBox {
private Label label;
private ListView<T> sourceList;
private ListView<T> targetList;
private ObservableList<T> source;
private ObservableList<T> target;
private SortedList<T> sourceSortedList;
private SortedList<T> targetSortedList;
private JFXButton addToTarget;
private JFXButton addAllToTarget;
private JFXButton removeFromTarget;
private JFXButton removeAllFromTarget;
public MultiList() {
super();
label = new Label();
getChildren().add(label);
HBox hbox = new HBox();
getChildren().add(hbox);
source = FXCollections.observableArrayList();
target = FXCollections.observableArrayList();
sourceSortedList = new SortedList<>(source);
targetSortedList = new SortedList<>(target);
sourceList = new ListView<>(sourceSortedList);
targetList = new ListView<>(targetSortedList);
sourceList.getStyleClass().add("multi-list");
targetList.getStyleClass().add("multi-list");
sourceList.prefHeightProperty().bindBidirectional(targetList.prefHeightProperty());
sourceList.prefWidthProperty().bindBidirectional(targetList.prefWidthProperty());
hbox.getChildren().add(sourceList);
VBox controls = new VBox();
controls.setAlignment(Pos.CENTER);
hbox.getChildren().add(controls);
addToTarget = new JFXButton();
addToTarget.setDisable(true);
addToTarget.setButtonType(ButtonType.FLAT);
addToTarget.getStyleClass().add("-fx-text-fill:#004679;-fx-font-size:14px;");
addToTarget.setText("ADD");
controls.getChildren().add(addToTarget);
addAllToTarget = new JFXButton();
addAllToTarget.setDisable(true);
addAllToTarget.setButtonType(ButtonType.FLAT);
addAllToTarget.getStyleClass().add("-fx-text-fill:#004679;-fx-font-size:14px;");
addAllToTarget.setText("ADD ALL");
controls.getChildren().add(addAllToTarget);
removeFromTarget = new JFXButton();
removeFromTarget.setDisable(true);
removeFromTarget.setButtonType(ButtonType.FLAT);
removeFromTarget.getStyleClass().add("-fx-text-fill:#004679;-fx-font-size:14px;");
removeFromTarget.setText("REMOVE");
controls.getChildren().add(removeFromTarget);
removeAllFromTarget = new JFXButton();
removeAllFromTarget.setDisable(true);
removeAllFromTarget.setButtonType(ButtonType.FLAT);
removeAllFromTarget.getStyleClass().add("-fx-text-fill:#004679;-fx-font-size:14px;");
removeAllFromTarget.setText("REMOVE ALL");
controls.getChildren().add(removeAllFromTarget);
targetList.getStyleClass().add("custom-jfx-list-intsys");
hbox.getChildren().add(targetList);
// intsysAvailable.depthProperty().set(1);
// intsysSelected.depthProperty().set(1);
sourceList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
targetList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
sourceList.getSelectionModel().selectedItemProperty().addListener((o, oldVal, newVal) -> {
if (newVal == null) {
addToTarget.setDisable(true);
} else {
addToTarget.setDisable(false);
}
});
sourceList.getItems().addListener((ListChangeListener.Change<? extends T> c) -> {
if (!c.getList().isEmpty()) {
addAllToTarget.setDisable(false);
} else {
addAllToTarget.setDisable(true);
}
});
targetList.getSelectionModel().selectedItemProperty().addListener((o, oldVal, newVal) -> {
if (newVal == null) {
removeFromTarget.setDisable(true);
} else {
removeFromTarget.setDisable(false);
}
});
targetList.getItems().addListener((ListChangeListener.Change<? extends T> c) -> {
if (!c.getList().isEmpty()) {
removeAllFromTarget.setDisable(false);
} else {
removeAllFromTarget.setDisable(true);
}
});
addToTarget.setOnMouseClicked((e) -> {
List<T> copy = new ArrayList<>(sourceList.getSelectionModel().getSelectedItems());
sourceList.getSelectionModel().clearSelection();
target.addAll(copy);
source.removeAll(copy);
});
addAllToTarget.setOnMouseClicked((e) -> {
List<T> copy = new ArrayList<>(source);
target.addAll(copy);
source.clear();
});
removeFromTarget.setOnMouseClicked((e) -> {
List<T> copy = new ArrayList<>(targetList.getSelectionModel().getSelectedItems());
targetList.getSelectionModel().clearSelection();
source.addAll(copy);
target.removeAll(copy);
});
removeAllFromTarget.setOnMouseClicked((e) -> {
List<T> copy = new ArrayList<>(target);
source.addAll(copy);
target.clear();
});
}
public void clear() {
source.clear();
target.clear();
}
public final String getPromptText() {
return label.getText();
}
public final void setPromptText(String value) {
label.setText(value);
}
public void setSourceSortComparator(Comparator<T> comparator) {
sourceSortedList.comparatorProperty().set(comparator);
}
public void setTargetSortComparator(Comparator<T> comparator) {
targetSortedList.comparatorProperty().set(comparator);
}
public ObservableList<T> getSourceItems() {
return source;
}
public ObservableList<T> getTargetItems() {
return target;
}
}
.multi-list {
-fx-background-color: white;
}
.multi-list .list-cell {
-fx-background-color: white;
}
.multi-list .list-cell:filled:selected {
-fx-background-color: rgba(0, 168, 200, 0.4);
}
<VBox fx:id="selection" spacing="10">
<Label>Test</Label>
<MultiList fx:id="test" />
</VBox>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment