如何使用Javafx获取TableView中多个选定行的值?

时间:2016-03-04 11:59:45

标签: javafx javafx-2 javafx-8

我为TableView Multiple Selection尝试了以下代码。我只想要一个ArrayList中所选行的所有ID。但每当我选择行而不是显示ID时,都会显示完整的行值。

package tabletest;

import java.util.ArrayList;
import java.util.ListIterator;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author vikassingh
 */
public class TableTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<ObservableList<String>> tableLocal = new TableView<>();
        tableLocal.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        TableColumn<ObservableList<String>, String> idCol = new TableColumn<>("ID");
        TableColumn<ObservableList<String>, String> nameCol = new TableColumn<>("Customer Name");

        tableLocal.getColumns().addAll(nameCol);

        //idCol
        idCol.setCellValueFactory(cellData -> {
            ObservableList<String> rowValue = cellData.getValue();
            String cellValue = rowValue.get(0);
            return new ReadOnlyStringWrapper(cellValue);
        });

        //nameCol
        nameCol.setCellValueFactory(cellData -> {
            ObservableList<String> rowValue = cellData.getValue();
            String cellValue = rowValue.get(1);
            return new ReadOnlyStringWrapper(cellValue);
        });

        tableLocal.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                ObservableList selectedItems = tableLocal.getSelectionModel().getSelectedItems();
                System.out.println(selectedItems);

                // TEST
                ArrayList<String> selectedIDs = new ArrayList<String>();
                for (int i = 0; i < selectedItems.size(); i++) {
                    ObservableList<String> innerSelectedItems = FXCollections.observableArrayList(selectedItems.get(i).toString());

                    for (String ID : innerSelectedItems) {
                        selectedIDs.add(ID);
                    }

                }

                ListIterator<String> iterator = selectedIDs.listIterator();
                while (iterator.hasNext()) {
                    System.out.println(iterator.next());
                }
                // END TEST
            }
        });

        ObservableList<String> row1 = FXCollections.observableArrayList();
        row1.add("1");
        row1.add("Cust One");
        ObservableList<String> row2 = FXCollections.observableArrayList();
        row2.add("2");
        row2.add("Cust Two");
        ObservableList<String> row3 = FXCollections.observableArrayList();
        row3.add("3");
        row3.add("Cust Three");
        ObservableList<ObservableList<String>> filteredDataLocal = FXCollections.observableArrayList();
        filteredDataLocal.addAll(row1, row2, row3);
        tableLocal.setItems(filteredDataLocal);

        VBox root = new VBox();
        root.getChildren().addAll(tableLocal);
        Scene scene = new Scene(root, 300, 300);

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

}

1 个答案:

答案 0 :(得分:0)

由于id是表示每行的列表中的第一个元素,因此只需要

tableLocal.setOnMousePressed(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        ObservableList<ObservableList<String>> selectedItems = tableLocal.getSelectionModel().getSelectedItems();
        System.out.println(selectedItems);

        // TEST
        ArrayList<String> selectedIDs = new ArrayList<String>();
        for (ObservableList<String> row : selectedItems) {
           selectedIDs.add(row.get(0));
        }

        ListIterator<String> iterator = selectedIDs.listIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        // END TEST
    }
});