首页 文章

javafx表格单元格onEditCommit事件未被触发?

提问于
浏览
0

我有一个TableView,它有很少的可编辑列 . 在JavaFX Scene Builder中,可编辑表列的 on Edit Commit 我映射了一个FXML控制器方法,它调用DAO服务从数据库返回数据 . 问题是在编辑表格单元格后没有调用事件处理程序方法 . 我希望在编辑单元格数据后按Tab键时触发此事件 . 这该怎么做?请建议

2 回答

  • 2

    我有同样的问题CheckBoxTableCell和DatePickerTableCell和ColorPickerTableCells :-(

    我这样处理它:在控件的事件中,我回到了“ ((Inputs)getTableView().getItems().get(getTableRow().getIndex() ”使用的POJO对象,我更新类似于在OnEditCommit方法中完成的...

    所以对我来说它看起来像这样(更新颜色):

    ((Inputs) getTableView().getItems().get(
                        getTableRow().getIndex())
                        ).setColor(cp.getValue());
    

    以下是ColorPickerCell的示例:

    public class ColorPickerTableCell<Inputs> extends TableCell<Inputs, Color>{
    private ColorPicker cp;
    
    public ColorPickerTableCell(){        
        cp = new ColorPicker(); 
        cp.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                commitEdit(cp.getValue());
                updateItem(cp.getValue(), isEmpty());
                ((Inputs) getTableView().getItems().get(
                        getTableRow().getIndex())
                        ).setColor(cp.getValue());
            }            
        });                
        setGraphic(cp);
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        setEditable(true);        
    }     
    @Override
    protected void updateItem(Color item, boolean empty) {
        super.updateItem(item, empty);
        cp.setVisible(!empty);
        this.setItem(item);
        cp.setValue(item);
    }
    }
    

    有了这个简单的JavaFX的POJO:

    public ObjectProperty<Color> color = new SimpleObjectProperty<Color>();
    
        this.color = new SimpleObjectProperty(color);
    
        public ObjectProperty<Color> colorProperty() {
        return color;
     }
    
    public void setColor(Color color2) {
        color.set(color2);
    }
    

    我不知道这是否是一个很好的方法来实现,但它对我有用...请注意,JavaFX的POJO只能在“ActionEvent”请求(组合框,日期选择器,颜色选择器等)中访问 .

    问候,

  • 0

    这是我用来从tableview的可编辑单元格调用DAO的方法 .

    private TableColumn<Person, String> createNameCol(){
        TableColumn col = new TableColumn("Name");
        col.setCellValueFactory(
                new PropertyValueFactory<Person, String>("name"));
        col.setCellFactory(TextFieldTableCell.forTableColumn());
        col.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<Person, String> t) {
    
                Person p = t.getRowValue();
                p.setName(t.getNewValue());
                sl.update(p); //  This is where I call the update method from my DAO.
            }
        });
    
        return col;
    }
    

    如果这不起作用,请发布您的代码 .

    EDIT:

    这是一个很好的tutorial for the editable tableViews

相关问题