How to add CheckBox's to a TableView in JavaFX

前端 未结 13 518
日久生厌
日久生厌 2020-11-30 00:32

In my Java Desktop Application I have a TableView in which I want to have a column with CheckBoxes.

I did find where this has been done http://www.jonathangiles.net/

13条回答
  •  旧巷少年郎
    2020-11-30 01:13

    This is the way is do it

    tbcSingleton.setCellValueFactory(data -> data.getValue().singletonProperty());
    tbcSingleton.setCellFactory( param -> {
        return new TableCell(){
            {
                setAlignment(Pos.CENTER);
            }
            protected void updateItem(Boolean item, boolean empty){
                if(!empty && item!=null) {
                    CheckBox cb = new CheckBox();
                    cb.setSelected(item);
                    cb.setFocusTraversable(false);
                    cb.selectedProperty().addListener((obs,old,niu)->listaFXMLController.get(getIndex()).setSingleton(niu));
                    setGraphic(cb);
                }else
                    setGraphic(null);
            }
        };
    });
    
    • cb.setFocusTraversable(false) is necesary to prevent focus getting stuck on it.

    • setGraphic(null) is necesary to erase anything left behind after deleting an item or whenever the source list changes

    • cb.selectedProperty().addListener((obs,old,niu)->(your stuff...)); this is where you catch the new value of the CheckBox and do whatever you want with it.

    Heres another one with a ToggleGroup and ToggleButtons

    tbcTipoControlador.setCellValueFactory(data -> data.getValue().controllerTypeProperty());
    tbcTipoControlador.setCellFactory( param -> {
        return new TableCell() {
            {
                setAlignment(Pos.CENTER);
            }
            protected void updateItem(ControllerType item, boolean empty){
                if(!empty && item!=null) {
                    ToggleButton tbModal = new ToggleButton("Modal");
                    tbModal.selectedProperty().addListener((obs,old,niu)->{
                        if(niu)
                            listaFXMLController.get(getIndex()).setControllerType(ControllerType.MODAL);
                    });
                    tbModal.setSelected(item.equals(ControllerType.MODAL));
                    ToggleButton tbPlain = new ToggleButton("Plain");
                    tbPlain.selectedProperty().addListener((obs,old,niu)->{
                        if(niu)
                            listaFXMLController.get(getIndex()).setControllerType(ControllerType.PLAIN);
                    });
                    tbPlain.setSelected(item.equals(ControllerType.PLAIN));
                    ToggleButton tbApplication= new ToggleButton("Application");
                    tbApplication.selectedProperty().addListener((obs,old,niu)->{
                        if(niu)
                            listaFXMLController.get(getIndex()).setControllerType(ControllerType.APPLICATION);
                    });
                    tbApplication.setSelected(item.equals(ControllerType.APPLICATION));
                    ToggleGroup gp = new ToggleGroup();
                    tbModal.setFocusTraversable(false);
                    tbPlain.setFocusTraversable(false);
                    tbApplication.setFocusTraversable(false);
                    tbModal.setPrefWidth(120);
                    tbPlain.setPrefWidth(120);
                    tbApplication.setPrefWidth(120);
                    gp.getToggles().addAll(tbModal,tbPlain,tbApplication);
                    HBox hb = new HBox();
                    hb.setAlignment(Pos.CENTER);
                    hb.getChildren().addAll(tbModal,tbPlain,tbApplication);
                    setGraphic(hb);
                }else
                    setGraphic(null);
            }
        };
    });
    

    I did some test and memory consumption is basically the same as using a ComboBoxTableCell

    This is how my little application looks (sry, my main language is Spanish and i build it for personal use)

提交回复
热议问题