0

I am looking for to remove the background of a checkbox because it takes space in a TableCell and it causes that the CheckBox is not centered.

As you can see if the background is marked red, it exceeds the checkbox and takes space.

Here is a minimal code you can verify it and the screenshoot:

Controller:

public class Controller implements Initializable {

    @FXML
    private TableColumn<Object,String> one;
    @FXML
    private TableView<Object> table;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        one.setCellFactory(cell -> new CheckBoxTableCell<Object, String>(){
            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if(empty){
                    return;
                }
                CheckBox graphic = (CheckBox) getGraphic();
                graphic.setStyle("-fx-background-color: red");
            }
        });
        table.getItems().add(new Object());
    }
}

enter image description here

I would like to eliminate the red part so only keeping the box itself.

I tried to reset the padding for it but it doesn't work. Can you help me?

Note: I won't accept setPadding(new Insets(0,-x,-y,0) so setting negative values to it to compensate the "error" only in that case if you can explain it why it is the only solution.

Sunflame
  • 2,993
  • 4
  • 24
  • 48
  • if I understand this correctly you want to center it then try wrapping it in a `HBox` or `VBox` that will be centering it – fuggerjaki61 Jan 20 '20 at 15:51
  • Yes and no, I want to remove that red part of the checkbox. – Sunflame Jan 20 '20 at 16:01
  • so it should fill the complete height and width – fuggerjaki61 Jan 20 '20 at 16:01
  • No...I don't want to fill the cell's height and width, just i don't want the red part which is the background of the `CheckBox`. – Sunflame Jan 20 '20 at 16:04
  • so you want the cell by minimized to fit the checkbox perfectly then see [this](https://stackoverflow.com/questions/14650787/javafx-column-in-tableview-auto-fit-size) – fuggerjaki61 Jan 20 '20 at 16:08
  • No... Please read the question again carefully and don't try to give me random answers, which won't help. – Sunflame Jan 20 '20 at 16:13
  • 2
    Note: For simply calling `#setStyle(String)` there's no need to cast to `CheckBox`. – Slaw Jan 21 '20 at 04:57
  • @Slaw Yeah, i know that, i just wanted to see the methods which the `CheckBox` has, maybe I can find something to remove the unwanted background, but I couldn't and I didn't change it back. But its a good point ;) – Sunflame Jan 21 '20 at 07:35

1 Answers1

2

The space(~1px) to the bottom is from the "padding" property and the space(~5px) to the right is from the label-padding property. So if you turn off the both, then you can get rid of the background.

CheckBox graphic = (CheckBox) getGraphic();
graphic.setStyle("-fx-background-color: red;-fx-label-padding:0px;-fx-padding:0px;");
Sai Dandem
  • 8,229
  • 11
  • 26
  • 1
    [_same note_](https://stackoverflow.com/questions/59826601/javafx-checkbox-unwanted-background#comment105804287_59826601) – Slaw Jan 21 '20 at 04:58