import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene; 
import javafx.scene.control.ContextMenu; 
import javafx.scene.control.MenuItem; 
import javafx.scene.control.SelectionMode; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class Mcve2 extends Application { 
@Override 
public void start(Stage stage) { 
TableView<ObservableList<String>> table = new TableView<ObservableList<String>>(); 
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); 

// Initializes a column and adds it to the table. 
TableColumn<ObservableList<String>, String> col = new TableColumn<ObservableList<String>, String>("Column"); 
col.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().get(0))); 
table.getColumns().add(col); 

// Add data to the table. 
table.getItems().add(FXCollections.observableArrayList("One")); 
table.getItems().add(FXCollections.observableArrayList("Two")); 
table.getItems().add(FXCollections.observableArrayList("Three")); 
table.getItems().add(FXCollections.observableArrayList("Four")); 

// Initializes a ContextMenu and adds a MenuItem to it. 
ContextMenu contextMenu = new ContextMenu(); 
table.setContextMenu(contextMenu); 
MenuItem menuItem = new MenuItem("Print selected rows"); 
// Add an EventHandler that will print the toString() of all selected 
// rows in the table. 
menuItem.setOnAction(e -> { 
// Retrieving an iterator from getSelectedItems() and invoking 
// next() on it will prevent the bug from happening. 
// table.getSelectionModel().getSelectedItems().iterator().next(); 
//Iterator iterator;
for (ObservableList<String> item : table.getSelectionModel().getSelectedItems()) { 
System.out.println(item); 
} 
}); 
contextMenu.getItems().add(menuItem); 

BorderPane view = new BorderPane(); 
view.setCenter(table); 

stage.setScene(new Scene(view)); 
stage.show(); 
} 

public static void main(String[] args) { 
launch(); 
} 
} 