public void start(Stage stage) { stage.setTitle("ListView demo"); FlowPane rootNode = new FlowPane(10, 10); rootNode.setAlignment(Pos.CENTER); Scene scene = new Scene(rootNode, 200, 120); stage.setScene(scene); responseLabel = new Label("Select Transport Type"); ObservableList<String> transportTypes = FXCollections.observableArrayList("Train", "Car", "Airplane"); ListView<String> transportListView = new ListView(transportTypes); transportListView.setPrefSize(80, 80); MultipleSelectionModel<String> listViewSelectionModel = transportListView.getSelectionModel(); listViewSelectionModel .selectedItemProperty() .addListener( new ChangeListener<String>() { public void changed( ObservableValue<? extends String> changed, String oldValue, String newValue) { responseLabel.setText("Transport selected is " + newValue); } }); rootNode.getChildren().addAll(transportListView, responseLabel); stage.show(); }
/** * FXML Controller class * * @author Mostafa Ali <*****@*****.**> */ public class InteractionsListController extends VBox { // Logger private static final Logger logger = LogManager.getLogger(); @FXML private TableView<InteractionState> InteractionTableView; @FXML private TableColumn<InteractionState, String> InteractionTableColumn; @FXML private TableColumn CheckTableColumn; CheckBox cb = new CheckBox(); ObservableList<InteractionState> interactions = FXCollections.observableArrayList(); public InteractionsListController() { logger.entry(); FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/customcontrol/InteractionsList.fxml")); fxmlLoader.setController(this); fxmlLoader.setRoot(this); try { fxmlLoader.load(); } catch (IOException ex) { logger.log(Level.FATAL, ex.getMessage(), ex); } logger.exit(); } public void setFddObjectModel(FddObjectModel fddObjectModel) { logger.entry(); if (fddObjectModel != null) { fddObjectModel .getInteractionClasses() .values() .stream() .forEach( (value) -> { interactions.add(new InteractionState(value)); }); InteractionTableView.setItems(interactions); interactions.forEach( (interaction) -> { interaction .onProperty() .addListener( (observable, oldValue, newValue) -> { if (!newValue) { cb.setSelected(false); } else if (interactions.stream().allMatch(a -> a.isOn())) { cb.setSelected(true); } }); }); InteractionTableColumn.setCellValueFactory(new PropertyValueFactory<>("interactionName")); CheckTableColumn.setCellValueFactory( new Callback< TableColumn.CellDataFeatures<InteractionState, Boolean>, ObservableValue<Boolean>>() { @Override public ObservableValue<Boolean> call( TableColumn.CellDataFeatures<InteractionState, Boolean> param) { return param.getValue().onProperty(); } }); CheckTableColumn.setCellFactory(CheckBoxTableCell.forTableColumn(CheckTableColumn)); cb.setUserData(CheckTableColumn); cb.setOnAction( (ActionEvent event) -> { CheckBox cb1 = (CheckBox) event.getSource(); TableColumn tc = (TableColumn) cb1.getUserData(); InteractionTableView.getItems() .stream() .forEach( (item) -> { item.setOn(cb1.isSelected()); }); }); CheckTableColumn.setGraphic(cb); } logger.exit(); } public List<InteractionClassFDD> getInteractions() { return interactions .stream() .filter(a -> a.isOn()) .map(a -> a.interaction) .collect(Collectors.toList()); } public static class InteractionState { private final ReadOnlyStringWrapper interactionName = new ReadOnlyStringWrapper(); private final BooleanProperty on = new SimpleBooleanProperty(); private final InteractionClassFDD interaction; public InteractionState(InteractionClassFDD interaction) { this.interaction = interaction; interactionName.set(interaction.getFullName()); } public String getInteractionName() { return interactionName.get(); } public ReadOnlyStringProperty interactionNameProperty() { return interactionName.getReadOnlyProperty(); } public boolean isOn() { return on.get(); } public void setOn(boolean value) { on.set(value); } public BooleanProperty onProperty() { return on; } @Override public String toString() { return interaction.getFullName(); } } }