Example usage for javafx.scene.control ListView ListView

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

Introduction

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

Prototype

public ListView(ObservableList<T> items) 

Source Link

Document

Creates a default ListView which will stack the contents retrieved from the provided ObservableList vertically.

Usage

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);//  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

@Override
public void start(Stage primaryStage) {

    ObservableList<String> data = FXCollections.observableArrayList();

    ListView<String> listView = new ListView<String>(data);
    listView.setPrefSize(200, 250);/*from  ww  w  .java2 s.co m*/
    listView.setEditable(true);

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

    listView.setItems(data);
    listView.setCellFactory((ListView<String> l) -> new ColorRectCell());
    StackPane root = new StackPane();
    root.getChildren().add(listView);
    primaryStage.setScene(new Scene(root, 200, 250));
    primaryStage.show();
}

From source file:Main.java

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

    ListView<String> listView = new ListView<String>(data);
    listView.setPrefSize(200, 250);//w  w  w .  ja v  a2  s.  co  m
    listView.setEditable(true);

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

    data.add("Double Click to Select Value");

    listView.setItems(data);
    listView.setCellFactory(ComboBoxListCell.forListView(names));

    StackPane root = new StackPane();
    root.getChildren().add(listView);
    primaryStage.setScene(new Scene(root, 200, 250));
    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);/*from   w w  w.  j a v a2 s.  c o 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:Main.java

private ListView<Person> createLeaderListView(ObservableList<Person> leaders) {
    final ListView<Person> leaderListView = new ListView<>(leaders);
    leaderListView.setPrefWidth(150);//from  w  ww  .  j  av a 2s.c om
    leaderListView.setMaxWidth(Double.MAX_VALUE);
    leaderListView.setPrefHeight(150);

    leaderListView.setCellFactory(new Callback<ListView<Person>, ListCell<Person>>() {
        @Override
        public ListCell<Person> call(ListView<Person> param) {
            Tooltip tooltip = new Tooltip();

            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);
                    }
                }
            };
            return cell;
        }
    });

    return leaderListView;
}

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  . j  a v  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);/* ww  w.  ja  v  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);

    // 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: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   w  ww. ja  v  a 2  s .c o 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:org.yccheok.jstock.gui.news.StockNewsJFrame.java

private void initComponents() {
    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    setAlwaysOnTop(true);/*from   w  w w  .j  a  v a2s  .  c om*/
    setAutoRequestFocus(false);
    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent evt) {
            formWindowClosing(evt);
        }
    });

    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosed(java.awt.event.WindowEvent evt) {
        }

        public void windowClosing(java.awt.event.WindowEvent evt) {
            NewsTask task = StockNewsJFrame.this.newsTask;
            if (task != null) {
                task.cancel(true);
            }
        }

        public void windowDeiconified(java.awt.event.WindowEvent evt) {
        }

        public void windowIconified(java.awt.event.WindowEvent evt) {
        }
    });

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            // JFXPanel => Scene => SplitPane:
            //      Left  (news List)       => StackPane => ListView / ProgressIndicator
            //      Right (HTML content)    => TabPane => Tab => StackPane => WebView / ProgressBar

            splitPane = new SplitPane();
            scene = new Scene(splitPane);

            scene.getStylesheets()
                    .add(StockNewsJFrame.class.getResource("StockNewsJFrame.css").toExternalForm());
            jfxPanel.setScene(scene);

            // Left component: News List
            messages_o = FXCollections.observableArrayList();
            newsListView = new ListView<>(messages_o);
            newsListView.setId("news-listview");

            stackPane.setId("parent-stackPane");
            stackPane.getChildren().addAll(newsListView, progressIn);

            splitPane.getItems().add(stackPane);
            SplitPane.setResizableWithParent(stackPane, Boolean.FALSE);

            // show progress indicator when loading
            progressIn.setMaxWidth(100);
            progressIn.setMaxHeight(100);
            progressIn.setVisible(true);
            newsListView.setVisible(true);

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

            // register event listener: add tab for news HTML content
            newsListView.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    if (event.getClickCount() >= 1) {
                        final FeedItem msg = newsListView.getSelectionModel().getSelectedItem();
                        if (msg == null) {
                            return;
                        }

                        final URL link = msg.getLink();
                        if (link == null || link.getHost() == null) {
                            return;
                        }

                        if (stockNewsContent == null) {
                            stackPane.setPrefWidth(stackPane.getWidth());
                            stackPane.setMaxWidth(stackPane.getWidth());
                            stackPane.setMinWidth(stackPane.getWidth() / 5);

                            try {
                                SwingUtilities.invokeAndWait(new Runnable() {
                                    @Override
                                    public void run() {
                                        // resize JFrame first
                                        StockNewsJFrame.this.setSize(fullSize.width, fullSize.height);

                                        java.awt.Insets in = StockNewsJFrame.this.getInsets();
                                        jfxPanel.setSize(StockNewsJFrame.this.getWidth() - in.left - in.right,
                                                jfxPanel.getHeight());

                                        java.awt.Insets in2 = jfxPanel.getInsets();

                                        // calculate width & height, but not resize in AWT event dispatching thread
                                        // javafx.scene.control.SplitPane should only be accessed from JavaFX Application Thread
                                        splitPaneWidth = jfxPanel.getWidth() - in2.left - in2.right;
                                        splitPaneHeight = jfxPanel.getHeight() - in2.top - in2.bottom;
                                    }
                                });
                            } catch (InterruptedException | InvocationTargetException ex) {
                                log.error(null, ex);
                            }

                            // resize to full screen size of jfxPanel
                            splitPane.resize(splitPaneWidth, splitPaneHeight);

                            stockNewsContent = new StockNewsContent();
                            splitPane.getItems().add(stockNewsContent.tabPane);
                            splitPane.setDividerPositions(0.5f);

                            stockNewsContent.tabPane.getSelectionModel().selectedItemProperty()
                                    .addListener(new ChangeListener<Tab>() {
                                        @Override
                                        public void changed(ObservableValue<? extends Tab> observable,
                                                Tab oldTab, Tab newTab) {
                                            int i = stockNewsContent.tabPane.getSelectionModel()
                                                    .getSelectedIndex();
                                            if (i < 0) {
                                                return;
                                            }

                                            jFrameTitle = stockNewsContent.tabsInfo.get(i).second;

                                            SwingUtilities.invokeLater(new Runnable() {
                                                @Override
                                                public void run() {
                                                    StockNewsJFrame.this.setTitle(jFrameTitle);
                                                }
                                            });
                                        }
                                    });
                        }
                        stockNewsContent.addNewsTab(link, StringEscapeUtils.unescapeHtml(msg.getTitle()));
                    }
                }
            });

            retrieveNewsInBackground();
        }
    });

    this.add(jfxPanel, BorderLayout.CENTER);
    this.setVisible(true);
}

From source file:qupath.lib.gui.panels.classify.RandomTrainingRegionSelector.java

private void createDialog() {
    dialog = new Stage();
    dialog.initOwner(qupath.getStage());
    dialog.setTitle("Training sample selector");

    pointCreator = new RandomPointCreator();

    for (PathClass pathClass : pathClassListModel) {
        if (pathClass != null && pathClass.getName() != null)
            pointCreator.addPathClass(pathClass,
                    KeyCode.getKeyCode(pathClass.getName().toUpperCase().substring(0, 1)));
        //            pointCreator.addPathClass(pathClass, KeyStroke.getKeyStroke(new pathClass.getName().toLowerCase().charAt(0), 0).getKeyCode());
    }//from   w  w w .  j  a va 2s .c  o  m
    //      PathClass tumourClass = PathClassFactory.getDefaultPathClass(PathClasses.TUMOR);
    //      PathClass stromaClass = PathClassFactory.getDefaultPathClass(PathClasses.STROMA);
    //      pointCreator.addPathClass(tumourClass, KeyCode.T);
    //      pointCreator.addPathClass(stromaClass, KeyCode.S);
    QuPathViewer viewer = qupath.getViewer();
    pointCreator.registerViewer(viewer);

    // Adapt to changing active viewers
    ImageDataChangeListener<BufferedImage> listener = new ImageDataChangeListener<BufferedImage>() {

        @Override
        public void imageDataChanged(ImageDataWrapper<BufferedImage> source,
                ImageData<BufferedImage> imageDataOld, ImageData<BufferedImage> imageDataNew) {
            if (pointCreator != null) {
                QuPathViewer viewer = qupath.getViewer();
                pointCreator.registerViewer(viewer);
                updateObjectCache(viewer);
            }
            refreshList();
            updateLabel();
        }

    };
    qupath.addImageDataChangeListener(listener);

    // Remove listeners for cleanup
    dialog.setOnCloseRequest(e -> {
        pointCreator.deregisterViewer();
        qupath.removeImageDataChangeListener(listener);
        dialog.setOnCloseRequest(null);
        dialog = null;
        // Re-enable mode switching
        qupath.setModeSwitchingEnabled(true);
    });

    ParameterPanelFX paramPanel = new ParameterPanelFX(params);
    paramPanel.getPane().setPadding(new Insets(2, 5, 5, 5));

    list = new ListView<PathClass>(pathClassListModel);
    list.setPrefSize(400, 200);

    // TODO: ADD A SENSIBLE RENDERER!
    // For now, this is simply duplicated from PathAnnotationPanel
    list.setCellFactory(new Callback<ListView<PathClass>, ListCell<PathClass>>() {

        @Override
        public ListCell<PathClass> call(ListView<PathClass> p) {

            ListCell<PathClass> cell = new ListCell<PathClass>() {

                @Override
                protected void updateItem(PathClass value, boolean bln) {
                    super.updateItem(value, bln);
                    int size = 10;
                    if (value == null) {
                        setText(null);
                        setGraphic(null);
                    } else if (value.getName() == null) {
                        setText("None");
                        setGraphic(new Rectangle(size, size, ColorToolsFX.getCachedColor(0, 0, 0, 0)));
                    } else {
                        setText(value.getName());
                        setGraphic(new Rectangle(size, size, ColorToolsFX.getPathClassColor(value)));
                    }
                }

            };

            return cell;
        }
    });

    //      list.setCellRenderer(new PathClassListCellRendererPoints());

    list.setTooltip(new Tooltip("Available classes"));

    labelCount = new Label();
    labelCount.setTextAlignment(TextAlignment.CENTER);
    labelCount.setPadding(new Insets(5, 0, 5, 0));
    BorderPane panelTop = new BorderPane();
    panelTop.setTop(paramPanel.getPane());
    panelTop.setCenter(list);
    panelTop.setBottom(labelCount);
    labelCount.prefWidthProperty().bind(panelTop.widthProperty());
    updateLabel();

    //      panelButtons.add(new JButton(new UndoAction("Undo")));
    Action actionAdd = new Action("Add to class", e -> {
        if (list == null || pointCreator == null)
            return;
        PathClass pathClass = list.getSelectionModel().getSelectedItem();
        pointCreator.addPoint(pathClass);
    });
    Action actionSkip = new Action("Skip", e -> {
        if (pointCreator != null)
            pointCreator.addPoint(null);
    });

    GridPane panelButtons = PanelToolsFX.createColumnGridControls(ActionUtils.createButton(actionAdd),
            ActionUtils.createButton(actionSkip));

    BorderPane pane = new BorderPane();
    pane.setCenter(panelTop);
    pane.setBottom(panelButtons);

    pane.setPadding(new Insets(10, 10, 10, 10));
    Scene scene = new Scene(pane);
    dialog.setScene(scene);
}