Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make checkbox or combobox readonly in JavaFX

How to make checkbox/combobox readonly in javaFX but not disabled.

I tried consuming onAction event but it didn't work.

checkBox.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        event.consume();
    }
});

Consuming all events like in code below works but I don't think it's a good solution:

checkBox.addEventFilter(KeyEvent.ANY, new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent event) {
        event.consume();
    }
});
checkBox.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEventevent) {
        event.consume();
    }
});
like image 880
yelliver Avatar asked Dec 02 '22 23:12

yelliver


1 Answers

You can set the check box to disabled but set the the look of it using CSS. If you are using the default style you can make the check box look 'normal' by setting full opacity.

checkbox.setStyle("-fx-opacity: 1");

It is probably a similar deal with the combo box.

like image 188
Andy Till Avatar answered Jan 17 '23 06:01

Andy Till