javafx之TableView的TaleCell

TaleCell

对TableColumn的cell里面弄重新构造
TableColumnsetCellFactory(TextFieldTableCell.forTableC
olumn());有一些默认的构造。 

或者重写TableCell类

 

[java] view plain copy
 
  1. tableColumn.setCellFactory(new Callback<TableColumn<Path, Number>, TableCell<Path, Number>>() {  
  2.             @Override  
  3.             public TableCell<Path, Number> call(TableColumn<Path, Number> param) {  
  4.                 return new MyTableCell<Path, Number>();    
  5.             }  
  6. }  


Tablecell里面不仅只存放文字,还可以存放其它Node,需要重写TableCell的update(T t,boolean empty)方法编辑单元格可以使用重写startEdit()和cancelEdit()

[java] view plain copy
 
  1. class MyTableCell<Path, Nodeextends TableCell<Path, Node> {  
  2.      protected void updateItem(Node node,boolean empty) {          
  3.               super.updateItem(node, empty);    
  4.               if (empty||node==null) {    //tableCell没有数据或者为空                         
  5.                 setText(null);    
  6.                 setGraphic(null);    
  7.               else {                                  
  8.                setText(null);    
  9.                setGraphic(node);  //设置Node  
  10.             }   
  11.      }  
  12.      @Override  
  13.    public void startEdit() {  
  14.        super.startEdit();    
  15.            // 设置编辑状态    
  16.        //super.setGraphic(null);  
  17.        //super.setText(null);  
  18.      }  
  19.   
  20.   
  21.     @Override  
  22.     public void cancelEdit() {  
  23.         super.cancelEdit();  
  24.         //退出编辑状态  
  25.         //super.setText(null);            
  26.         // super.setGraphic(null);  
  27.     }  
  28. }  



双击鼠标监听

通过tableColumn.setCellFactory(new TaskCellFactory());设置了CellFactory。

TaskCellFactory的内容如下:

[java] view plain copy
 
  1. class TaskCellFactory implements Callback<TableColumn<Task, String>, TableCell<Task, String>> {  
  2.   
  3.     @Override  
  4.     public TableCell<Task, String> call(TableColumn<Task, String> param) {  
  5.         TextFieldTableCell<Task, String> cell = new TextFieldTableCell<>();  
  6.         cell.setOnMouseClicked((MouseEvent t) -> {  
  7.             if (t.getClickCount() == 2) {  
  8.                //双击执行的代码  
  9.             }  
  10.         });        
  11.         return cell;  
  12.     }  
  13. }  
整个实现的核心就在于重点就在于实现Callback<TableColumn<Task, String>, TableCell<Task, String>>然后返回JavaFX API自带的TextFieldTableCell。并在call()方法中,为cell增加了双击事件的处理。

原文地址:https://www.cnblogs.com/maokun/p/6710832.html