Javafx, get the object referenced by a TableCell

前端 未结 1 1219
梦如初夏
梦如初夏 2020-12-16 03:18

I have the following Callback listening on the selected Cell of a TableView:

        Callback, TableCell

        
相关标签:
1条回答
  • 2020-12-16 04:08

    The MyFTPFile object is associated with the cell's row, so, as the asker pointed out in his comment, it is retrievable via cell.getTableRow().getItem().

    At first I thought this should be cell.getItem(), which returns the data value associated with the cell. However, most of the time, the cell data value will be a property of the backing item rather than the object itself (for example a filename field of a MyFTPFile object).

    Executable sample for the curious:

    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 +
              '}';
        }
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题