使用colorpicker编辑器的JavaFX tableview [英] JavaFX tableview with colorpicker editor

查看:157
本文介绍了使用colorpicker编辑器的JavaFX tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TableView,它使用ColorPicker来(显示/编辑)单元格中的颜色。
该表在所需字段中显示ColorPicker,但编辑不起作用。

I have a TableView that uses a ColorPicker to (display/edit) colors in a cell. The table display the ColorPicker in the desired field, but edits aren't working.

TableColumn<SeriesPreferences, Color> c2 = new TableColumn<SeriesPreferences, Color>("Color");
c2.setCellValueFactory(new PropertyValueFactory<SeriesPreferences, Color>("color"));
c2.setCellFactory(new Callback<TableColumn<SeriesPreferences, Color>,
                                TableCell<SeriesPreferences, Color>>()
    {
        @Override
        public TableCell<SeriesPreferences, Color> 
        call(final TableColumn<SeriesPreferences, Color> param)
        {
            TableCell<SeriesPreferences, Color> cell = 
                new TableCell<SeriesPreferences, Color>()
                    {
                        @Override
                        public void updateItem(Color c, boolean empty)
                        {
                            if(c != null)
                            {
                                final ColorPicker cp = new ColorPicker();
                                cp.setValue(c);
                                setGraphic(cp);
                                cp.setOnAction(new EventHandler<javafx.event.ActionEvent>()
                                    {
                                        public void 
                                        handle(javafx.event.ActionEvent t)
                                        {
                                            getTableView().edit(getTableRow().getIndex(), param);
                                            commitEdit(cp.getValue());
                                        }
                                    });
                            }
                        }
                    };
            return cell;
        }
    });

c2.setOnEditCommit(new EventHandler<CellEditEvent<SeriesPreferences, Color>>()
    {
        @Override
        public void handle(CellEditEvent<SeriesPreferences, Color> t)
        {
            ((SeriesPreferences) t.getTableView().getItems().get(t.getTablePosition().
                                                    getRow())).setColor(t.getNewValue());
        }
    });

当我更改颜色选择器中的颜色时,没有调用编辑事件处理程序,任何想法?

The edit event handler isn't being called when i change the color in the color picker, any ideas?

推荐答案

如果JavaFX POJO(或JavaFX Bean)的属性正确绑定到表格,则无需直接访问它除了commitEdit之外,没有必要调用任何东西。

There's no need to access the JavaFX POJO (or JavaFX Bean) directly if its properties are correctly bound to the table and also it isn't necessary to call anything other than commitEdit.

Max Beikirch的回答是误导性的,因为它会导致颜色选择器(以及它的颜色)当表未处于编辑模式时消失。这是一种将表格置于编辑模式的解决方法,但这是一个糟糕的模式。所以在点击按钮显示颜色选择器弹出窗口之前这样做:

The answer from Max Beikirch is misleading, because it causes the color picker (and with it the color) to disappear when the table is not in edit mode. It's a workaround to put the table into edit mode, but a bad one. So do this before showing the color picker popup when click on the button:

用这样的颜色选择器写你的单元格:

Write your cell with a color picker like this:

public class ColorTableCell<T> extends TableCell<T, Color> {    
    private final ColorPicker colorPicker;

    public ColorTableCell(TableColumn<T, Color> column) {
        this.colorPicker = new ColorPicker();
        this.colorPicker.editableProperty().bind(column.editableProperty());
        this.colorPicker.disableProperty().bind(column.editableProperty().not());
        this.colorPicker.setOnShowing(event -> {
            final TableView<T> tableView = getTableView();
            tableView.getSelectionModel().select(getTableRow().getIndex());
            tableView.edit(tableView.getSelectionModel().getSelectedIndex(), column);       
        });
        this.colorPicker.valueProperty().addListener((observable, oldValue, newValue) -> {
            if(isEditing()) {
                commitEdit(newValue);
            }
        });     
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    }

    @Override
    protected void updateItem(Color item, boolean empty) {
        super.updateItem(item, empty);  

        setText(null);  
        if(empty) {     
            setGraphic(null);
        } else {        
            this.colorPicker.setValue(item);
            this.setGraphic(this.colorPicker);
        } 
    }
}

如果您使用的是Java 7,用匿名内部类替换lambdas,但它也应该工作。完整的博客文章是这里

If you're on Java 7, replace the lambdas with anonymous inner classes, but it should work as well. Full blog post is here.

这篇关于使用colorpicker编辑器的JavaFX tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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