Example usage for javafx.scene.control ListView getSelectionModel

List of usage examples for javafx.scene.control ListView getSelectionModel

Introduction

In this page you can find the example usage for javafx.scene.control ListView getSelectionModel.

Prototype

public final MultipleSelectionModel<T> getSelectionModel() 

Source Link

Document

Returns the currently installed selection model.

Usage

From source file:Main.java

@Override
public void start(Stage stage) {
    VBox box = new VBox();
    Scene scene = new Scene(box, 200, 200);
    stage.setScene(scene);//from w w  w.j ava 2 s  . com
    stage.setTitle("ListViewSample");
    box.getChildren().addAll(list, label);
    VBox.setVgrow(list, Priority.ALWAYS);

    label.setLayoutX(10);
    label.setLayoutY(115);
    label.setFont(Font.font("Verdana", 20));

    list.setItems(data);

    list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
        @Override
        public ListCell<String> call(ListView<String> list) {
            return new ColorRectCell();
        }
    });

    list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
        public void changed(ObservableValue<? extends String> ov, String old_val, String new_val) {
            label.setText(new_val);
            label.setTextFill(Color.web(new_val));
        }
    });
    stage.show();
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    ObservableList<String> data = FXCollections.observableArrayList();

    ListView<String> listView = new ListView<String>(data);
    listView.setPrefSize(200, 250);//from  www  .j  a v a  2s . c o  m

    data.addAll("A", "B", "C", "D", "E");

    listView.setItems(data);
    listView.getSelectionModel().selectedItemProperty()
            .addListener((ObservableValue<? extends String> ov, String old_val, String new_val) -> {
                System.out.println(new_val);

            });
    StackPane root = new StackPane();
    root.getChildren().add(listView);
    primaryStage.setScene(new Scene(root, 200, 250));
    primaryStage.show();
}

From source file:Main.java

private void initializeListView(ListView<Student> listView) {
    listView.setPrefSize(250, 290);/*from w  w  w.j  a  v a2s  .c  om*/
    listView.setEditable(false);
    listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    listView.setCellFactory(new StringListCellFactory());
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 400, 250, Color.WHITE);

    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(5));
    gridpane.setHgap(10);/* w w  w  .jav  a 2 s .c  o  m*/
    gridpane.setVgap(10);
    ColumnConstraints column1 = new ColumnConstraints(150, 150, Double.MAX_VALUE);
    ColumnConstraints column2 = new ColumnConstraints(50);
    ColumnConstraints column3 = new ColumnConstraints(150, 150, Double.MAX_VALUE);
    column1.setHgrow(Priority.ALWAYS);
    column3.setHgrow(Priority.ALWAYS);
    gridpane.getColumnConstraints().addAll(column1, column2, column3);

    Label candidatesLbl = new Label("Candidates");
    GridPane.setHalignment(candidatesLbl, HPos.CENTER);
    gridpane.add(candidatesLbl, 0, 0);

    Label selectedLbl = new Label("selected");
    gridpane.add(selectedLbl, 2, 0);
    GridPane.setHalignment(selectedLbl, HPos.CENTER);

    // Candidates
    final ObservableList<String> candidates = FXCollections.observableArrayList("Z", "A", "B", "C", "D");
    final ListView<String> candidatesListView = new ListView<>(candidates);
    gridpane.add(candidatesListView, 0, 1);

    final ObservableList<String> selected = FXCollections.observableArrayList();
    final ListView<String> heroListView = new ListView<>(selected);
    gridpane.add(heroListView, 2, 1);

    Button sendRightButton = new Button(" > ");
    sendRightButton.setOnAction((ActionEvent event) -> {
        String potential = candidatesListView.getSelectionModel().getSelectedItem();
        if (potential != null) {
            candidatesListView.getSelectionModel().clearSelection();
            candidates.remove(potential);
            selected.add(potential);
        }
    });

    Button sendLeftButton = new Button(" < ");
    sendLeftButton.setOnAction((ActionEvent event) -> {
        String s = heroListView.getSelectionModel().getSelectedItem();
        if (s != null) {
            heroListView.getSelectionModel().clearSelection();
            selected.remove(s);
            candidates.add(s);
        }
    });
    VBox vbox = new VBox(5);
    vbox.getChildren().addAll(sendRightButton, sendLeftButton);

    gridpane.add(vbox, 1, 1);
    root.setCenter(gridpane);

    GridPane.setVgrow(root, Priority.ALWAYS);
    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 400, 250, Color.WHITE);

    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(5));
    gridpane.setHgap(10);/*from w w w. java2 s. com*/
    gridpane.setVgap(10);

    ColumnConstraints column1 = new ColumnConstraints(150, 150, Double.MAX_VALUE);
    ColumnConstraints column2 = new ColumnConstraints(50);
    ColumnConstraints column3 = new ColumnConstraints(150, 150, Double.MAX_VALUE);

    column1.setHgrow(Priority.ALWAYS);
    column3.setHgrow(Priority.ALWAYS);

    gridpane.getColumnConstraints().addAll(column1, column2, column3);

    // Candidates label
    Label candidatesLbl = new Label("Candidates");
    GridPane.setHalignment(candidatesLbl, HPos.CENTER);
    gridpane.add(candidatesLbl, 0, 0);

    // Heroes label
    Label heroesLbl = new Label("Letters");
    gridpane.add(heroesLbl, 2, 0);
    GridPane.setHalignment(heroesLbl, HPos.CENTER);

    final ObservableList<String> candidates = FXCollections.observableArrayList("A", "B", "C", "D");

    final ListView<String> candidatesListView = new ListView<>(candidates);
    gridpane.add(candidatesListView, 0, 1);

    final ObservableList<String> heroes = FXCollections.observableArrayList();
    final ListView<String> heroListView = new ListView<>(heroes);
    gridpane.add(heroListView, 2, 1);

    Button sendRightButton = new Button(" > ");
    sendRightButton.setOnAction((ActionEvent event) -> {
        String potential = candidatesListView.getSelectionModel().getSelectedItem();
        if (potential != null) {
            candidatesListView.getSelectionModel().clearSelection();
            candidates.remove(potential);
            heroes.add(potential);
        }
    });

    Button sendLeftButton = new Button(" < ");
    sendLeftButton.setOnAction((ActionEvent event) -> {
        String notHero = heroListView.getSelectionModel().getSelectedItem();
        if (notHero != null) {
            heroListView.getSelectionModel().clearSelection();
            heroes.remove(notHero);
            candidates.add(notHero);
        }
    });

    // place the buttons
    VBox vbox = new VBox(5);
    vbox.setAlignment(Pos.CENTER);
    vbox.getChildren().addAll(sendRightButton, sendLeftButton);

    GridPane.setHalignment(vbox, HPos.CENTER);
    gridpane.add(vbox, 1, 1);

    // place the grid
    root.setCenter(gridpane);
    GridPane.setVgrow(root, Priority.ALWAYS);
    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 500, 250, Color.WHITE);

    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(5));
    gridpane.setHgap(10);// www  .j a  va2s .  c om
    gridpane.setVgap(10);
    root.setCenter(gridpane);

    ObservableList<Person> leaders = getPeople();
    final ObservableList<Person> teamMembers = FXCollections.observableArrayList();

    ListView<Person> leaderListView = createLeaderListView(leaders);
    TableView<Person> employeeTableView = createEmployeeTableView(teamMembers);

    Label bossesLbl = new Label("Boss");
    GridPane.setHalignment(bossesLbl, HPos.CENTER);
    gridpane.add(bossesLbl, 0, 0);
    gridpane.add(leaderListView, 0, 1);

    Label emplLbl = new Label("Employees");
    GridPane.setHalignment(emplLbl, HPos.CENTER);
    gridpane.add(emplLbl, 2, 0);
    gridpane.add(employeeTableView, 2, 1);

    leaderListView.getSelectionModel().selectedItemProperty()
            .addListener((ObservableValue<? extends Person> observable, Person oldValue, Person newValue) -> {
                if (observable != null && observable.getValue() != null) {
                    teamMembers.clear();
                    teamMembers.addAll(observable.getValue().employeesProperty());
                }
            });
    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("ObservableLists");
    Group root = new Group();
    Scene scene = new Scene(root, 400, 250, Color.WHITE);

    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(5));
    gridpane.setHgap(10);/*w w w.  j  a va2s .  co  m*/
    gridpane.setVgap(10);

    Label candidatesLbl = new Label("Left");
    GridPane.setHalignment(candidatesLbl, HPos.CENTER);
    gridpane.add(candidatesLbl, 0, 0);

    Label heroesLbl = new Label("Right");
    gridpane.add(heroesLbl, 2, 0);
    GridPane.setHalignment(heroesLbl, HPos.CENTER);

    final ObservableList<String> lefts = FXCollections.observableArrayList("A", "B", "C");
    final ListView<String> leftListView = new ListView<String>(lefts);
    leftListView.setPrefWidth(150);
    leftListView.setPrefHeight(150);

    gridpane.add(leftListView, 0, 1);

    final ObservableList<String> rights = FXCollections.observableArrayList();
    final ListView<String> rightListView = new ListView<String>(rights);
    rightListView.setPrefWidth(150);
    rightListView.setPrefHeight(150);

    gridpane.add(rightListView, 2, 1);

    Button sendRightButton = new Button(">");
    sendRightButton.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
            String item = leftListView.getSelectionModel().getSelectedItem();
            if (item != null) {
                leftListView.getSelectionModel().clearSelection();
                lefts.remove(item);
                rights.add(item);
            }
        }
    });

    Button sendLeftButton = new Button("<");
    sendLeftButton.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
            String item = rightListView.getSelectionModel().getSelectedItem();
            if (item != null) {
                rightListView.getSelectionModel().clearSelection();
                rights.remove(item);
                lefts.add(item);
            }
        }
    });

    VBox vbox = new VBox(5);
    vbox.getChildren().addAll(sendRightButton, sendLeftButton);

    gridpane.add(vbox, 1, 1);
    GridPane.setConstraints(vbox, 1, 1, 1, 2, HPos.CENTER, VPos.CENTER);

    root.getChildren().add(gridpane);
    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:Person.java

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 500, 250, Color.WHITE);
    // create a grid pane
    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(5));
    gridpane.setHgap(10);//from ww  w.j  a  va  2 s .  co m
    gridpane.setVgap(10);

    ObservableList<Person> leaders = FXCollections.observableArrayList();
    leaders.add(new Person("A", "B", "C"));
    leaders.add(new Person("D", "E", "F"));
    final ListView<Person> leaderListView = new ListView<Person>(leaders);
    leaderListView.setPrefWidth(150);
    leaderListView.setPrefHeight(150);

    // 
    leaderListView.setCellFactory(new Callback<ListView<Person>, ListCell<Person>>() {

        public ListCell<Person> call(ListView<Person> param) {
            final Label leadLbl = new Label();
            final Tooltip tooltip = new Tooltip();
            final ListCell<Person> cell = new ListCell<Person>() {
                @Override
                public void updateItem(Person item, boolean empty) {
                    super.updateItem(item, empty);
                    if (item != null) {
                        leadLbl.setText(item.getAliasName());
                        setText(item.getFirstName() + " " + item.getLastName());
                        tooltip.setText(item.getAliasName());
                        setTooltip(tooltip);
                    }
                }
            }; // ListCell
            return cell;
        }
    }); // setCellFactory

    gridpane.add(leaderListView, 0, 1);

    leaderListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>() {
        public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) {
            System.out.println("selection changed");
        }
    });

    root.getChildren().add(gridpane);

    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:com.chart.SwingChart.java

/**
 * Series edition//from ww w  .  j  a v  a2  s .  co m
 * @param series Series to edit
 */
void editSeries(final Series series) {
    String[] style = series.getStyle().split(";");
    String strColor = "black";
    final TextField editWidth = new TextField();

    String tempS = "null";
    for (String e : style) {
        if (e.contains("color: ")) {
            strColor = e.replace("color: ", "");
        } else if (e.contains("width: ")) {
            editWidth.setText(e.replace("width: ", ""));
        } else if (e.contains("shape: ")) {
            tempS = e.replace("shape: ", "");
        }
    }
    final String symbol = tempS;

    final List<SeriesShape> symbolList = new ArrayList<>();
    final ObservableList<SeriesShape> symbolListModel;
    final ListView<SeriesShape> comboSymbol = new ListView();
    symbolList.add(new SeriesShape("null", javafx.scene.paint.Color.web(strColor)));
    symbolList.add(new SeriesShape("rectangle", javafx.scene.paint.Color.web(strColor)));
    symbolList.add(new SeriesShape("circle", javafx.scene.paint.Color.web(strColor)));
    symbolList.add(new SeriesShape("triangle", javafx.scene.paint.Color.web(strColor)));
    symbolList.add(new SeriesShape("crux", javafx.scene.paint.Color.web(strColor)));
    symbolList.add(new SeriesShape("diamond", javafx.scene.paint.Color.web(strColor)));
    symbolList.add(new SeriesShape("empty rectangle", javafx.scene.paint.Color.web(strColor)));
    symbolList.add(new SeriesShape("empty circle", javafx.scene.paint.Color.web(strColor)));
    symbolList.add(new SeriesShape("empty triangle", javafx.scene.paint.Color.web(strColor)));
    symbolList.add(new SeriesShape("empty diamond", javafx.scene.paint.Color.web(strColor)));

    symbolListModel = FXCollections.observableList(symbolList);
    comboSymbol.setItems(symbolListModel);
    comboSymbol.setCellFactory(new Callback<ListView<SeriesShape>, ListCell<SeriesShape>>() {
        @Override
        public ListCell<SeriesShape> call(ListView<SeriesShape> p) {
            ListCell<SeriesShape> cell = new ListCell<SeriesShape>() {
                @Override
                protected void updateItem(SeriesShape t, boolean bln) {
                    super.updateItem(t, bln);
                    if (t != null) {
                        setText("");
                        setGraphic(t.getShapeGraphic());
                    }
                }
            };

            return cell;
        }
    });
    for (SeriesShape smb : symbolListModel) {
        if (smb.getName().equals(symbol)) {
            comboSymbol.getSelectionModel().select(smb);
        }
    }

    final ColorPicker colorPicker = new ColorPicker(javafx.scene.paint.Color.web(strColor));

    colorPicker.setOnAction((ActionEvent t) -> {
        String sc = colorPicker.getValue().toString();
        symbolListModel.clear();
        symbolListModel.add(new SeriesShape("null", javafx.scene.paint.Color.web(sc)));
        symbolListModel.add(new SeriesShape("rectangle", javafx.scene.paint.Color.web(sc)));
        symbolListModel.add(new SeriesShape("circle", javafx.scene.paint.Color.web(sc)));
        symbolListModel.add(new SeriesShape("triangle", javafx.scene.paint.Color.web(sc)));
        symbolListModel.add(new SeriesShape("crux", javafx.scene.paint.Color.web(sc)));
        symbolListModel.add(new SeriesShape("diamond", javafx.scene.paint.Color.web(sc)));
        symbolListModel.add(new SeriesShape("empty rectangle", javafx.scene.paint.Color.web(sc)));
        symbolListModel.add(new SeriesShape("empty circle", javafx.scene.paint.Color.web(sc)));
        symbolListModel.add(new SeriesShape("empty triangle", javafx.scene.paint.Color.web(sc)));
        symbolListModel.add(new SeriesShape("empty diamond", javafx.scene.paint.Color.web(sc)));

        comboSymbol.setItems(symbolListModel);
        for (SeriesShape smb : symbolListModel) {
            if (smb.getName().equals(symbol)) {
                comboSymbol.getSelectionModel().select(smb);
            }
        }
    });

    GridPane grid = new GridPane();

    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(0, 10, 0, 10));

    grid.add(new Label("Series"), 0, 0);
    grid.add(new Label(series.getKey().toString()), 1, 0);
    grid.add(new Label("Color"), 0, 1);
    grid.add(colorPicker, 1, 1);
    grid.add(new Label("Width"), 0, 2);
    grid.add(editWidth, 1, 2);
    grid.add(new Label("Shape"), 0, 3);
    grid.add(comboSymbol, 1, 3);

    new PseudoModalDialog(skeleton, grid, true) {
        @Override
        public boolean validation() {
            String strColor = colorPicker.getValue().toString();
            String strWidth = editWidth.getText();
            double dWidth = Double.valueOf(strWidth);
            String strSimbolo = "null";
            SeriesShape simb = new SeriesShape(comboSymbol.getSelectionModel().getSelectedItem().toString(),
                    javafx.scene.paint.Color.web(strColor));

            XYItemRenderer renderer = (XYItemRenderer) plot.getRenderer(series.getAxisIndex());

            renderer.setSeriesPaint(series.getSeriesIndex(), scene2awtColor(colorPicker.getValue()));

            try {
                if (Double.valueOf(strWidth) > 0) {
                    ((XYLineAndShapeRenderer) renderer).setSeriesLinesVisible(series.getSeriesIndex(), true);
                    renderer.setSeriesStroke(series.getSeriesIndex(),
                            new BasicStroke(Integer.valueOf(strWidth)));
                } else {
                    ((XYLineAndShapeRenderer) renderer).setSeriesLinesVisible(series.getSeriesIndex(), false);
                }
            } catch (NumberFormatException ex) {

            }

            if (simb.getName().contains("null")) {
                ((XYLineAndShapeRenderer) renderer).setSeriesShapesVisible(series.getSeriesIndex(), false);
                renderer.setSeriesShape(series.getSeriesIndex(), null);
            } else {
                ((XYLineAndShapeRenderer) renderer).setSeriesShapesVisible(series.getSeriesIndex(), true);
                renderer.setSeriesShape(series.getSeriesIndex(), simb.getShapeAWT());
                if (simb.getName().contains("empty")) {
                    ((XYLineAndShapeRenderer) renderer).setSeriesShapesFilled(series.getSeriesIndex(), false);
                } else {
                    ((XYLineAndShapeRenderer) renderer).setSeriesShapesFilled(series.getSeriesIndex(), true);
                }

            }

            series.setStyle(
                    "color: " + strColor + ";width: " + editWidth.getText() + ";shape: " + strSimbolo + ";");

            for (Node le : legendFrame.getChildren()) {
                if (le instanceof LegendAxis) {
                    for (Node nn : ((LegendAxis) le).getChildren()) {
                        if (nn instanceof Label) {
                            if (((Label) nn).getText().equals(series.getKey().toString())) {
                                ((Label) nn).setGraphic(simb.getShapeGraphic());
                            }
                        }
                    }
                }
            }
            return true;
        }
    }.show();

}

From source file:org.nmrfx.processor.gui.PropertyManager.java

PropertyManager(ProcessorController processorController, final ListView scriptView, PropertySheet propertySheet,
        ObservableList<String> listItems, TextField opTextField, PopOver popOver) {
    this.processorController = processorController;
    this.scriptView = scriptView;
    this.listItems = listItems;
    this.opTextField = opTextField;
    this.propertySheet = propertySheet;
    this.popOver = popOver;
    doubleListener = new ChangeListener<Number>() {
        @Override/*ww  w. ja  v  a  2 s  . co m*/
        public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
            updateOp((PropertySheet.Item) observableValue);
        }
    };
    intListener = new ChangeListener<Number>() {
        @Override
        public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
            updateOp((PropertySheet.Item) observableValue);
        }
    };
    boolListener = new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observableValue, Boolean bool, Boolean bool2) {
            updateOp((PropertySheet.Item) observableValue);
        }
    };
    stringListener = new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observableValue, String string, String string2) {
            updateOp((PropertySheet.Item) observableValue);
        }
    };
    complexListener = new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observableValue, String c1, String c2) {
            updateOp((PropertySheet.Item) observableValue);
        }
    };
    listListener = new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observableValue, String c1, String c2) {
            updateOp((PropertySheet.Item) observableValue);
        }
    };

    ChangeListener<Number> scriptOpListener = new ChangeListener<Number>() {
        @Override
        public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
            int scriptIndex = (Integer) number2;
            try {
                if ((listItems.size() == 1) && (scriptIndex == -1)) {
                    //there is a single item in the Cells -- Don't unselect
                    scriptIndex = 0;
                } else if (scriptIndex != -1) {
                    String selOp = (String) scriptView.getItems().get(scriptIndex);
                    setPropSheet(scriptIndex, selOp);
                } else {
                    setPropSheet(scriptIndex, "");
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                currentIndex = scriptIndex;
            }
        }
    };
    scriptView.getSelectionModel().selectedIndexProperty().addListener(scriptOpListener);

    propertySheet.setPropertyEditorFactory(new NvFxPropertyEditorFactory(processorController));
    propertySheet.setMode(PropertySheet.Mode.NAME);
    propertySheet.setModeSwitcherVisible(false);
    propertySheet.setSearchBoxVisible(false);

}