Javafx,获取TableCell引用的对象 [英] Javafx, get the object referenced by a TableCell

查看:142
本文介绍了Javafx,获取TableCell引用的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TableView的选定单元格上侦听以下回调:

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

        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对象,该对象被双击,以便我可以将其传递给另一个类并进行处理...任何想法如何做??

Now, I would like to get the MyFTPFile object which is referenced by the cell, which is doubleclicked, so that i can pass it to another class and do stuff... Any Idea how to do that???

谢谢.

推荐答案

MyFTPFile对象与单元格的行关联,因此,正如提问者在其注释中指出的那样,可以通过cell.getTableRow().getItem()对其进行检索.

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().

起初,我认为这应该是 cell.getItem(),它返回与单元格关联的数据值.但是,在大多数情况下,单元格数据值将是后备项目的属性,而不是对象本身(例如MyFTPFile对象的文件名字段).

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).

好奇的可执行示例:

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 +
          '}';
    }
  }

}

这篇关于Javafx,获取TableCell引用的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆