2016-03-04 82 views
0

我已經嘗試了以下代碼爲TableView多選。我只想在ArrayList中選擇所有行的ID。但是,無論何時選擇行而不是顯示ID,都會顯示完整的行值。如何使用Javafx獲取TableView中多個選定行的值?

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

這些嵌套的for循環看起來像一個很好的方式,以降低性能... – fabian

+0

什麼表格中每一行的對象類型?你能爲它顯示類定義嗎? –

+0

@James_D:它ObservableList ,我從SQLite數據庫檢索數據。以上代碼是與我的項目具有相同輸出的示例代碼。 –

回答

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 
    } 
}); 
相關問題