javaFX选择mutlipe行表格tableview,放入HashMap

时间:2015-07-16 18:10:19

标签: java javafx

我目前有一个tableview,允许用户进行单一选择。我想允许用户从tableview中进行多项选择,这将在hashMap中设置为键,我将如何更改当前代码以允许此操作。任何帮助赞赏。

@FXML
TableView<Run> BookingRunTable;

@FXML
TableColumn<Run, String> BookingRunName;

@FXML
TableColumn<Run, Character> BookingRunSize;

@FXML
TableColumn<Run, Double> BookingRunPrice;

@Override
public void initialize(URL location, ResourceBundle resources) {
    availableRuns = FXCollections.observableArrayList(availableRunList());

    BookingRunName.setCellValueFactory(new PropertyValueFactory<Run, String>("RunName"));
    BookingRunSize.setCellValueFactory(new PropertyValueFactory<Run, Character>("RunType"));
    BookingRunPrice.setCellValueFactory(new PropertyValueFactory<Run, Double>("pricePerNight"));

    BookingRunTable.setItems(availableRuns);
    BookingRunTable.getSelectionModel().selectedItemProperty()
            .addListener(new ChangeListener<Run>() {
                // sets the selected properties of run to detail containers
                @Override
                public void changed(
                        ObservableValue<? extends Run> observable,
                        Run oldValue, Run newValue) {
                    checkRequiredInfo();
                }
            });

    // Then i want the selected rows of the tableview to be put into a Map as the key, so later i can assign other values to the key. 
    HashMap<Integer, Integer> bookingDogRunMap = new HashMap<Integer, Integer>();

    bookingDogRunMap.put(BookingRunTable.getSelectionModel().getSelectedItem().getRunID(), 0);
}

1 个答案:

答案 0 :(得分:0)

要在initialize()方法调用

中允许多项选择
bookingRunTable.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

要处理所有选定的行,请使用selectionModel的{​​{3}}方法(注意复数形式)。例如:

for (Run run : bookingRunTable.getSelectionMode().getSelectedItems()) {
    bookingDogRunMap.put(run.getRunID(), 0);
}