如何从列表视图中选择多个项目 - JavaFX 8 [英] How to select multiple items from a List view - JavaFX 8

查看:415
本文介绍了如何从列表视图中选择多个项目 - JavaFX 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用 JavaFX 的新手,我正在尝试将 ObservableList 添加到表视图中。
该列表仅包含String。

I'm new at using JavaFX and I'm trying to add an ObservableList to a table view. The list contains only String.

我的目标是显示已连接设备的列表,并让用户选择执行操作(1或更多),有没有更好的方法来实现这一目标?

My goals is to show list of connected devices and let the user choose on which to perform the action (1 or more), is there any better way to achieve this?

编辑:
我已经导航到ListView,现在它显示了列表,如何从所选项目创建新列表?

Ive chaned to ListView and now it shows the list, how can I create a new list from the selected Items ?

推荐答案

以下是基于您的评论的示例

Here's an example based on your comments

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ListSelect extends Application {

    @Override
    public void start(Stage stage) {
        ObservableList<String> items = FXCollections.observableArrayList(
                "one","two","three","four","five","six","seven");
        ListView<String> list = new ListView<>(items);
        ListView<String> selected = new ListView<>();
        HBox root = new HBox(list, selected);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();

        //set this to SINGLE to allow selecting just one item
        list.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        list.getSelectionModel().selectedItemProperty().addListener((obs,ov,nv)->{
            selected.setItems(list.getSelectionModel().getSelectedItems());
        });
    }

    public static void main(String[] args) {launch(args);}

}

这篇关于如何从列表视图中选择多个项目 - JavaFX 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆