cell button di tableview

kali ini saya beri tutor bagaimana memberi cell button seperti gambar di bawah ini :


Di gambar diatas sudah jelas bahwa adanya tombol delete dan edit di dalam cell tableview. oke tutor kali ini juga masih menyambung dari crud javafx.

  • tambahkan skrip ini di biodataController.java
  • 01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    private class ButtonCell extends TableCell<Object, Boolean> {
            final Hyperlink cellButtonDelete = new Hyperlink("Delete");
            final Hyperlink cellButtonEdit = new Hyperlink("Edit");
            final HBox hb = new HBox(cellButtonDelete,cellButtonEdit);
            ButtonCell(final TableView tblView){
                hb.setSpacing(4);
                 
                //cell delete
                cellButtonDelete.setOnAction((ActionEvent t) -> {
                    statusKlik = "1";
                    int row = getTableRow().getIndex();
                    tableData.getSelectionModel().select(row);
                    klikTableData(null);
                    modelBiodata m = new modelBiodata();
                    m.setId(txtId.getText());
                    crudData.delete(m);
                    tampilData();
                    clear();
                    autoId();
                    dialog(Alert.AlertType.INFORMATION, "Data Berhasil Dihapus");
                    statusKlik = "0";
                    StatusKode = "0";
                });
                 
                //cell edit
                cellButtonEdit.setOnAction((ActionEvent event) -> {
                    statusKlik = "1";
                    int row = getTableRow().getIndex();
                    tableData.getSelectionModel().select(row);
                    klikTableData(null);
                    statusKlik = "0";
                });
            }
     
            @Override
            protected void updateItem(Boolean t, boolean empty) {
                super.updateItem(t, empty);
                if(!empty){
                    setGraphic(hb);
                }else{
                    setGraphic(null);
                }
            }
        }
    private class ButtonCell extends TableCell<Object, Boolean> {
            final Hyperlink cellButtonDelete = new Hyperlink("Delete");
            final Hyperlink cellButtonEdit = new Hyperlink("Edit");
            final HBox hb = new HBox(cellButtonDelete,cellButtonEdit);
            ButtonCell(final TableView tblView){
                hb.setSpacing(4);
                
                //cell delete
                cellButtonDelete.setOnAction((ActionEvent t) -> {
                    statusKlik = "1";
                    int row = getTableRow().getIndex();
                    tableData.getSelectionModel().select(row);
                    klikTableData(null);
                    modelBiodata m = new modelBiodata();
                    m.setId(txtId.getText());
                    crudData.delete(m);
                    tampilData();
                    clear();
                    autoId();
                    dialog(Alert.AlertType.INFORMATION, "Data Berhasil Dihapus");
                    statusKlik = "0";
                    StatusKode = "0";
                });
                
                //cell edit
                cellButtonEdit.setOnAction((ActionEvent event) -> {
                    statusKlik = "1";
                    int row = getTableRow().getIndex();
                    tableData.getSelectionModel().select(row);
                    klikTableData(null);
                    statusKlik = "0";
                });
            }
    
            @Override
            protected void updateItem(Boolean t, boolean empty) {
                super.updateItem(t, empty);
                if(!empty){
                    setGraphic(hb);
                }else{
                    setGraphic(null);
                }
            }
        }
    
  • dan tambahkan di constructornya
  • 01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    colAction.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Object,Boolean>,ObservableValue<Boolean>>() {
          @Override
          public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Object,Boolean> p) {
                return new SimpleBooleanProperty(p.getValue() != null);
          }
    });
      
    colAction.setCellFactory(new Callback<TableColumn<Object, Boolean>,TableCell<Object, Boolean>>() {
          @Override
          public TableCell<Object, Boolean> call(TableColumn<Object, Boolean> p) {
                return new ButtonCell(tableData);
          }
    });
    colAction.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Object,Boolean>,ObservableValue<Boolean>>() {
          @Override
          public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Object,Boolean> p) {
                return new SimpleBooleanProperty(p.getValue() != null);
          }
    });
     
    colAction.setCellFactory(new Callback<TableColumn<Object, Boolean>,TableCell<Object, Boolean>>() {
          @Override
          public TableCell<Object, Boolean> call(TableColumn<Object, Boolean> p) {
                return new ButtonCell(tableData);
          }
    });
    


    Download Full Source Code biodata.zip
    3 komentar :

    3 komentar :