简体   繁体   中英

JavaFX Color Picker Custom Color In Table Cell

I'm currently using JavaFX ColorPicker to select colors in my application. The ones outside the TableView work as expected, but I've run into an issue with the ones contained in the TableView .

I'm currently using the solution found at Michael Simons's site to implement custom controls in a TableCell . It works fine... until you open the Custom Color dialog of the Color Picker. At this point, any interaction with the custom color dialog box closes and commits the edit, meaning you're unable to, as an example, key in an RGB code for specific color usage.

How do I keep this open until the custom color value is committed from this dialog?

Found a solution; hopefully it can help someone else searching:

The original solution used a change listener to commit the edited table cell:

this.colorPicker.valueProperty().addListener((observable, oldValue, newValue) -> {
    if(isEditing()) {
    commitEdit(newValue);
    }
});

However, the ColorPicker custom color window continuously updates the value as you drag the sliders or target around. As a result, the first time you'd click, the listener would fire and you'd end up out of the dialog.

Changing this to an event listener against the isHiding event fixes this problem. The ColorPicker hides when you select a preset color or when you click [Save] or [Use] in the custom color window. Replace the above snippet with this, and you're good to go!

this.colorPicker.setOnHiding(event -> {
    if (isEditing()) {
        commitEdit(this.colorPicker.getValue());
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM