Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX tableview with colorpicker editor

Tags:

java

javafx-2

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?

like image 450
José Avatar asked Aug 28 '12 23:08

José


People also ask

How to get Color from ColorPicker JavaFX?

ColorPicker is a part of JavaFX. ColorPicker allows the user to choose a color from given set of colors or make their own custom color. An initial Color can be set using the setValue() function or defining it in a constructor and the color selected by the user can be found using the getValue() function.

How do you populate a table in JavaFX?

The most important classes for creating tables in JavaFX applications are TableView , TableColumn , and TableCell . You can populate a table by implementing the data model and by applying a cell factory. The table classes provide built-in capabilities to sort data in columns and to resize columns when necessary.

What does TableView refresh do?

Calling refresh() forces the TableView control to recreate and repopulate the cells necessary to populate the visual bounds of the control. In other words, this forces the TableView to update what it is showing to the user.


1 Answers

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.

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);
        } 
    }
}

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

like image 95
Michael Simons Avatar answered Oct 14 '22 13:10

Michael Simons