2014-11-24 52 views
2

在對TableView進行排序或過濾之前,有沒有辦法在TableView中獲取選定行的原始索引?如果不是,我可以選擇製作自己的TableRow對象,選擇它並獲取它並具有方法getOriginalRowIdex()?在TableView中獲取選定的TableRows原始索引

代碼tableView.getSelectionModel().getSelectedIndex());根據排序和過濾的數據返回選定的索引,這使得行索引與列表中的索引不可能匹配。

tableView.setRowFactory(tv -> { 
      TableRow<ObservableList> row = new TableRow<>(); 
      row.setOnMouseClicked(event -> { 
       if (event.getClickCount() == 2 && (!row.isEmpty())) { 
        label.setText(rowMessages.get(tableView.getSelectionModel().getSelectedIndex())); 

        System.out.println(tableView.getSelectionModel().getSelectedIndex()); 

       } 
      }); 
      return row; 
     }); 
+0

爲了什麼目的,您需要解析原始索引?通常情況下,您需要的所有內容都應該與您的模型元素一起存儲,即在TableView中顯示,因此您不必執行其他查找。 – eckig 2014-11-24 18:54:17

+0

我不使用模型,因爲數據是動態添加的(來自數據庫)。我有一條消息應該對每一行都是唯一的。 – miniHessel 2014-11-24 19:02:17

+0

我有一條消息應該對每一行都是唯一的,我將所有消息存儲在一個字符串列表中。如果我有原始行索引,我可以做list.get(originalIndex) – miniHessel 2014-11-24 19:08:50

回答

1

FilteredListSortedList只是包裝了一個普通ObservableList。由於您必須擁有原始列表,只需使用list.indexOf()查找該行中數據的索引即可。

public class FilteredTable extends Application { 
public static void main(String[] args){launch(args);} 
    @Override 
    public void start(Stage stage) { 
     ObservableList<LineItem> items = FXCollections.observableArrayList(); 
     for (int i = 0;i<10;i++){items.add(new LineItem(i+"'th", i));} 

     TableView tableView = new TableView(); 
     FilteredList<LineItem> evens = new FilteredList<>(items, p->p.amountProperty().get()%2==0); 
     SortedList<LineItem> sorted = new SortedList<>(evens); 
     sorted.comparatorProperty().bind(tableView.comparatorProperty()); 
     tableView.setItems(sorted); 

     TableColumn<LineItem,String> descCol = new TableColumn<>("desc"); 
     descCol.setCellValueFactory(new PropertyValueFactory<>("desc")); 

     TableColumn<LineItem, Double> amountCol = new TableColumn<>("amount"); 
     amountCol.setCellValueFactory(new PropertyValueFactory<>("amount")); 

     Label label = new Label("click a row"); 
     tableView.setRowFactory(tv -> { 
      TableRow<ObservableList> row = new TableRow<>(); 
      row.setOnMouseClicked(event -> { 
       if (event.getClickCount() == 2 && (!row.isEmpty())) { 
        label.setText(tableView.getSelectionModel().getSelectedIndex() 
          +" <-tbl row, idx in items-> " 
          +items.indexOf(tableView.getSelectionModel().getSelectedItem())); 
       } 
      }); 
      return row; 
     }); 

     tableView.getColumns().addAll(descCol,amountCol); 

     stage.setScene(new Scene(new VBox(5,tableView,label),300,300)); 
     stage.show(); 
    } 


    public class LineItem { 

     private final StringProperty desc = new SimpleStringProperty(); 
     private final IntegerProperty amount = new SimpleIntegerProperty(); 

     public StringProperty descProperty() {return desc;} 
     public IntegerProperty amountProperty() {return amount;} 

     public LineItem(String dsc, int amt) { 
      desc.set(dsc); amount.set(amt); 
     } 
    } 

} 
+0

謝謝,這個作品很棒。 – miniHessel 2014-11-26 07:57:17

+0

或者對過濾/排序列表使用'getSourceIndex(int)'方法。 – 2015-10-18 00:04:16

相關問題