Pergunta

I am trying to get the selection from a TableView in JavaFX 2.0. I have stored 5 Persons (5 rows) in the TableView. The code to get the selection model is:

TableView<Person> tableView =
myStage.getTableView();

ObservableList<Person> selection = 
tableView.getSelectionModel().getSelectedItems();

System.out.println(selection.size());

Now when I select multiple rows and then execute the method including the above code, the following prints the selection * 2 and sometimes the selection * 3. For example: I select all 5 rows and it prints out a size of 10 and sometimes 15!

What am I doing wrong here?

Foi útil?

Solução

There is a bug with TableView returning duplicated items for selection made by shift-click. As a workaround until fix you can try filter duplicated items by:

Set<Person> selection = new HashSet<Person>(tableView.getSelectionModel().getSelectedItems());
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top