2012-11-27 20 views
7

我有以下的回調在TableView中的選定單元格聽:的JavaFX,得到引用的對象由TableCell的

 Callback<TableColumn<MyFTPFile,String>, TableCell<MyFTPFile,String>> cellFactory = 
      new Callback<TableColumn<MyFTPFile,String>, TableCell<MyFTPFile,String>>() { 
       public TableCell<MyFTPFile,String> call(TableColumn<MyFTPFile,String> p) { 
        TableCell<MyFTPFile,String> cell = new TableCell<MyFTPFile, String>() { 
         @Override 
         public void updateItem(String item, boolean empty) { 
          super.updateItem(item, empty); 
          setText(empty ? null : getString()); 
          setGraphic(null); 
         } 

         private String getString() { 
          return getItem() == null ? "" : getItem().toString(); 
         } 
        }; 

        cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { 
         @Override 
         public void handle(MouseEvent event) { 
          if (event.getClickCount() > 1) { 
           TableCell<MyFTPFile,String> c = (TableCell<MyFTPFile,String>) event.getSource(); 


           ftpObservablelist = MyFTPClient.getInstance().getFtpObservableList(); 
           ftpTable.setItems(ftpObservablelist); 

          } 
         } 
        }); 

現在,我想獲得它是由細胞所引用的MyFTPFile對象,這是doubleclicked,所以我可以傳遞給另一個類,做東西...任何想法如何做到這一點?

在此先感謝。

回答

10

MyFTPFile對象與單元格的行相關聯,因此,正如提問者在他的評論中指出的那樣,它可以通過cell.getTableRow().getItem()進行檢索。

起初我以爲這應該是cell.getItem(),它返回與單元格關聯的數據值。但是,大多數情況下,單元格數據值將成爲後備項目的屬性,而不是對象本身(例如MyFTPFile對象的文件名字段)。

爲好奇可執行文件樣本:

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.TableCell; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.control.cell.TextFieldTableCell; 
import javafx.scene.input.MouseEvent; 
import javafx.stage.Stage; 
import javafx.util.Callback; 

public class TableClickListener extends Application { 

    public static void main(String[] args) { 
    launch(args); 
    } 

    class FTPTableCell<S, T> extends TextFieldTableCell<S, T> { 
    FTPTableCell() { 
     super(); 
     addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent event) { 
      if (event.getClickCount() > 1 && getItem() != null) { 
      System.out.println("Sending " + getTableRow().getItem() + " to the FTP client"); 
      } 
     } 
     }); 
    } 
    } 

    final Callback<TableColumn<MyFTPFile, String>, TableCell<MyFTPFile, String>> FTP_TABLE_CELL_FACTORY = 
     new Callback<TableColumn<MyFTPFile, String>, TableCell<MyFTPFile, String>>() { 
     public TableCell<MyFTPFile, String> call(TableColumn<MyFTPFile, String> p) { 
      return new FTPTableCell<>(); 
     } 
     }; 

    @Override 
    public void start(final Stage stage) { 
    final TableView<MyFTPFile> table = new TableView<>(); 

    final TableColumn<MyFTPFile, String> filenameColumn = new TableColumn<>("Filename"); 
    filenameColumn.setCellValueFactory(new PropertyValueFactory<MyFTPFile, String>("filename")); 
    filenameColumn.setCellFactory(FTP_TABLE_CELL_FACTORY); 
    filenameColumn.setMinWidth(150); 

    final TableColumn<MyFTPFile, String> ratingColumn = new TableColumn<>("Rating"); 
    ratingColumn.setCellValueFactory(new PropertyValueFactory<MyFTPFile, String>("rating")); 
    ratingColumn.setCellFactory(FTP_TABLE_CELL_FACTORY); 
    ratingColumn.setMinWidth(20); 

    table.getColumns().setAll(filenameColumn, ratingColumn); 

    table.getItems().setAll(
     new MyFTPFile("xyzzy.txt", 10), 
     new MyFTPFile("management_report.doc", 1), 
     new MyFTPFile("flower.png", 7) 
    ); 

    table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 

    stage.setScene(new Scene(new Group(table))); 
    stage.show(); 
    } 

    public class MyFTPFile { 
    private final String filename; 
    private final int rating; 

    MyFTPFile(String filename, int rating) { 
     this.filename = filename; 
     this.rating = rating; 
    } 

    public String getFilename() { 
     return filename; 
    } 

    public int getRating() { 
     return rating; 
    } 

    @Override 
    public String toString() { 
     return "MyFTPFile{" + 
      "filename='" + filename + '\'' + 
      ", rating=" + rating + 
      '}'; 
    } 
    } 

} 
+2

THX,但它是cell.getTableRow()getItem()時,我必須使用,來獲取對象。 – Ilir

+1

所以它是:-)。 。 。答案已糾正。 – jewelsea

相關問題