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

    Javafx - Implementasi dari database ke BarChart

    Selamat siang agan dan aganwati sekalian, kali ini ane akan beri tutor lagi, bagaimana mengimplementasikan BarChart di javafx dan tutor kali ini masih menyambung dari tutor sebelumnya yaitu CRUD JAVAFX . tool yang dibutuhkan yaitu:

    Netbeans
    Scene Builder
    Paham MVC pattern
    Gorengan dan kopi

    Kira2 Penampakannya seperti Dibawah ini :
       
         1. yang ini adalah grafik chart tahun kelahiran. 

         2. yang ini dari tutor kemarin, tetapi ane telah beri tab yang ditandai merah..

    • Pertama - tama bikin file .fxml baru, desain dengan Scene Builder Seperti Dibawah ini :



  • modelGrafik.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
    45
    46
    47
    48
    49
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.model;
     
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
     
    /**
     *
     * @author herudi-pc
     */
    public class modelGrafik {
        private final StringProperty tahun = new SimpleStringProperty();
        private final IntegerProperty jumlahNama = new SimpleIntegerProperty();
         
     
        public modelGrafik() {
        }
     
        public String getTahun() {
            return tahun.get();
        }
     
        public void setTahun(String value) {
            tahun.set(value);
        }
     
        public StringProperty tahunProperty() {
            return tahun;
        }
         
        public Integer getJumlahNama() {
            return jumlahNama.get();
        }
     
        public void setJumlahNama(Integer value) {
            jumlahNama.set(value);
        }
     
        public IntegerProperty jumlahNamaProperty() {
            return jumlahNama;
        }
         
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.model;
    
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    
    /**
     *
     * @author herudi-pc
     */
    public class modelGrafik {
        private final StringProperty tahun = new SimpleStringProperty();
        private final IntegerProperty jumlahNama = new SimpleIntegerProperty();
        
    
        public modelGrafik() {
        }
    
        public String getTahun() {
            return tahun.get();
        }
    
        public void setTahun(String value) {
            tahun.set(value);
        }
    
        public StringProperty tahunProperty() {
            return tahun;
        }
        
        public Integer getJumlahNama() {
            return jumlahNama.get();
        }
    
        public void setJumlahNama(Integer value) {
            jumlahNama.set(value);
        }
    
        public IntegerProperty jumlahNamaProperty() {
            return jumlahNama;
        }
        
    }
    
  • interGrafik.java
  • 01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.interfaces;
     
    import biodata.model.modelGrafik;
    import javafx.collections.ObservableList;
     
    /**
     *
     * @author herudi-pc
     */
    public interface interGrafik {
        ObservableList<modelGrafik> getTahunKelahiran();
        ObservableList<Object> tahunKelahiranToGrafik();
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.interfaces;
    
    import biodata.model.modelGrafik;
    import javafx.collections.ObservableList;
    
    /**
     *
     * @author herudi-pc
     */
    public interface interGrafik {
        ObservableList<modelGrafik> getTahunKelahiran();
        ObservableList<Object> tahunKelahiranToGrafik();
    }
    
  • implGrafik.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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.implement;
     
    import biodata.interfaces.interGrafik;
    import biodata.koneksi.koneksi;
    import biodata.model.modelGrafik;
    import java.sql.ResultSet;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.chart.XYChart;
     
     
    public class implGrafik implements interGrafik {
        koneksi k;
     
        @Override
        public ObservableList<modelGrafik> getTahunKelahiran() {
            k = new koneksi();
            ObservableList<modelGrafik> listData = FXCollections.observableArrayList();
            try {
                String sql = "select distinct(extract(year from tanggalLahir)) as tahun, "
                            + "count(nama) as jumlahNama "
                            + "from tablebiodata "
                            + "group by tahun "
                            + "order by tahun";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {      
                    modelGrafik m = new modelGrafik();
                    m.setTahun(rs.getString("tahun"));
                    m.setJumlahNama(rs.getInt("jumlahNama"));
                    listData.add(m);
                }
            } catch (Exception e) {
            }
            return listData;
        }
     
        @Override
        public ObservableList<Object> tahunKelahiranToGrafik() {
            ObservableList<Object> barCar = FXCollections.observableArrayList();
            try {
                k = new koneksi();
                String sql = "select distinct(extract(year from tanggalLahir)) as tahun, "
                            + "count(nama) as jumlahNama "
                            + "from tablebiodata "
                            + "group by tahun "
                            + "order by tahun";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {      
                    XYChart.Series<String, Integer> aSeries = new XYChart.Series<>();
                    aSeries.getData().add(new XYChart.Data(rs.getString("tahun"), rs.getInt("jumlahNama")));
                    barCar.add(aSeries);
                }
            } catch (Exception e) {
            }
            return barCar;
        }
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.implement;
    
    import biodata.interfaces.interGrafik;
    import biodata.koneksi.koneksi;
    import biodata.model.modelGrafik;
    import java.sql.ResultSet;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.chart.XYChart;
    
    
    public class implGrafik implements interGrafik {
        koneksi k;
    
        @Override
        public ObservableList<modelGrafik> getTahunKelahiran() {
            k = new koneksi();
            ObservableList<modelGrafik> listData = FXCollections.observableArrayList();
            try {
                String sql = "select distinct(extract(year from tanggalLahir)) as tahun, "
                            + "count(nama) as jumlahNama "
                            + "from tablebiodata "
                            + "group by tahun "
                            + "order by tahun";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {       
                    modelGrafik m = new modelGrafik();
                    m.setTahun(rs.getString("tahun"));
                    m.setJumlahNama(rs.getInt("jumlahNama"));
                    listData.add(m);
                }
            } catch (Exception e) {
            }
            return listData;
        }
    
        @Override
        public ObservableList<Object> tahunKelahiranToGrafik() {
            ObservableList<Object> barCar = FXCollections.observableArrayList();
            try {
                k = new koneksi();
                String sql = "select distinct(extract(year from tanggalLahir)) as tahun, "
                            + "count(nama) as jumlahNama "
                            + "from tablebiodata "
                            + "group by tahun "
                            + "order by tahun";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {       
                    XYChart.Series<String, Integer> aSeries = new XYChart.Series<>();
                    aSeries.getData().add(new XYChart.Data(rs.getString("tahun"), rs.getInt("jumlahNama")));
                    barCar.add(aSeries);
                }
            } catch (Exception e) {
            }
            return barCar;
        }
    }
    
  • grafikController.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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.controller;
     
    import biodata.implement.implGrafik;
    import biodata.interfaces.interGrafik;
    import biodata.model.modelGrafik;
    import de.jensd.fx.fontawesome.AwesomeDude;
    import de.jensd.fx.fontawesome.AwesomeIcon;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.StackedBarChart;
    import javafx.scene.control.Button;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
     
    /**
     * FXML Controller class
     *
     * @author herudi-pc
     */
    public class grafikController implements Initializable {
        @FXML
        private TableView<modelGrafik> tableDetail;
        @FXML
        private TableColumn<modelGrafik, String> colDetailTahun;
        @FXML
        private TableColumn<modelGrafik, String> colDetailJumlah;
        @FXML
        private StackedBarChart bar;
        @FXML
        private NumberAxis barY;
        @FXML
        private CategoryAxis barX;
        @FXML
        private Button btnRefresh;
        ObservableList<Object> dataGrafik = FXCollections.observableArrayList();
        ObservableList<modelGrafik> dataDetail = FXCollections.observableArrayList();
        interGrafik crudGrafik = new implGrafik();
     
        /**
         * Initializes the controller class.
         * @param url
         * @param rb
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            colDetailJumlah.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelGrafik, String> cellData) ->
                            cellData.getValue().jumlahNamaProperty().asString());
            colDetailTahun.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelGrafik, String> cellData) ->
                            cellData.getValue().tahunProperty());
            AwesomeDude.setIcon(btnRefresh, AwesomeIcon.CHAIN_BROKEN, "15px");
            tampilData();
            // TODO
        
         
        private void tampilData(){
            dataDetail = crudGrafik.getTahunKelahiran();
            dataGrafik = crudGrafik.tahunKelahiranToGrafik();
            bar.setData(dataGrafik);
            tableDetail.setItems(dataDetail);
        }
         
        @FXML
        private void aksiRefresh(ActionEvent e){
            bar.setAnimated(true);
            barY.setAnimated(true);
            barX.setAnimated(false);
            tampilData();
        }
         
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.controller;
    
    import biodata.implement.implGrafik;
    import biodata.interfaces.interGrafik;
    import biodata.model.modelGrafik;
    import de.jensd.fx.fontawesome.AwesomeDude;
    import de.jensd.fx.fontawesome.AwesomeIcon;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.StackedBarChart;
    import javafx.scene.control.Button;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    
    /**
     * FXML Controller class
     *
     * @author herudi-pc
     */
    public class grafikController implements Initializable {
        @FXML
        private TableView<modelGrafik> tableDetail;
        @FXML
        private TableColumn<modelGrafik, String> colDetailTahun;
        @FXML
        private TableColumn<modelGrafik, String> colDetailJumlah;
        @FXML
        private StackedBarChart bar;
        @FXML
        private NumberAxis barY;
        @FXML
        private CategoryAxis barX;
        @FXML
        private Button btnRefresh;
        ObservableList<Object> dataGrafik = FXCollections.observableArrayList();
        ObservableList<modelGrafik> dataDetail = FXCollections.observableArrayList();
        interGrafik crudGrafik = new implGrafik();
    
        /**
         * Initializes the controller class.
         * @param url
         * @param rb
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            colDetailJumlah.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelGrafik, String> cellData) ->
                            cellData.getValue().jumlahNamaProperty().asString());
            colDetailTahun.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelGrafik, String> cellData) ->
                            cellData.getValue().tahunProperty());
            AwesomeDude.setIcon(btnRefresh, AwesomeIcon.CHAIN_BROKEN, "15px");
            tampilData();
            // TODO
        }  
        
        private void tampilData(){
            dataDetail = crudGrafik.getTahunKelahiran();
            dataGrafik = crudGrafik.tahunKelahiranToGrafik();
            bar.setData(dataGrafik);
            tableDetail.setItems(dataDetail);
        }
        
        @FXML
        private void aksiRefresh(ActionEvent e){
            bar.setAnimated(true);
            barY.setAnimated(true);
            barX.setAnimated(false);
            tampilData();
        }
        
    }
    
  • di biodataController.java tambahkan source ini di awal inisialisasi
  • 01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    //tab biodata
    @FXML
    private Tab tabBiodata;
     
    //tab grafik
    @FXML
    private Tab tabGrafikKelahiran;
     
    //load anchorPane yang ada di tab ke 2
    @FXML
    private AnchorPane paneLoadGrafik;
    //tab biodata
    @FXML
    private Tab tabBiodata;
    
    //tab grafik
    @FXML
    private Tab tabGrafikKelahiran;
    
    //load anchorPane yang ada di tab ke 2
    @FXML
    private AnchorPane paneLoadGrafik;
    
  • masih di biodataController.java buat perintah ini untuk load grafik.fxml dan tampilkan di constructor
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    //load view grafik.fxml
    private void loadGrafik(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            Parent p = fxmlLoader.load(getClass().getResourceAsStream("/biodata/view/grafik.fxml"));
            paneLoadGrafik.getChildren().add(p);  
        } catch (IOException e) {
        }  
    }
    //load view grafik.fxml
    private void loadGrafik(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            Parent p = fxmlLoader.load(getClass().getResourceAsStream("/biodata/view/grafik.fxml"));
            paneLoadGrafik.getChildren().add(p);   
        } catch (IOException e) {
        }   
    }
    
    oke ane rasa sudah jelas, bila ada yang ditanyakan silahkan tanyakan agar tidak tersesat dikamar :D

    Crud JavaFX



    Kali ini ane akan memberi tutor bagaimana crud (create, read, update, delete) di javafx. mungkin tutor ini sudah agak basi, tapi menurut ane para programmer java banyak yang belum tahu. oke langsung saja. untuk persiapan :

    1. Netbeans 
    2. Xammp
    3. Scene Builder
    4. Paham MVC pattern
    5. kopi, susu, cemilan, rokok :)

    • membuat Database Dan Table
    1
    CREATE DATABASE `biodata` /*!40100 COLLATE 'latin1_swedish_ci' */;
    CREATE DATABASE `biodata` /*!40100 COLLATE 'latin1_swedish_ci' */;
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    CREATE TABLE `tablebiodata` (
      `id` VARCHAR(5) NOT NULL,
     `nama` VARCHAR(50) NULL DEFAULT NULL,
     `alamat` VARCHAR(50) NULL DEFAULT NULL,
     `tanggalLahir` DATE NULL DEFAULT NULL,
     PRIMARY KEY (`id`)
    )
    COLLATE='latin1_swedish_ci'
    ENGINE=InnoDB;
    CREATE TABLE `tablebiodata` (
      `id` VARCHAR(5) NOT NULL,
     `nama` VARCHAR(50) NULL DEFAULT NULL,
     `alamat` VARCHAR(50) NULL DEFAULT NULL,
     `tanggalLahir` DATE NULL DEFAULT NULL,
     PRIMARY KEY (`id`)
    )
    COLLATE='latin1_swedish_ci'
    ENGINE=InnoDB;
    


    • Buka Netbeans Dan Buat Project



    • Buat package dengan urutan seperti ini




    • Desain View Dengan Scene Builder seperti gambar dibawah ini.


    jangan lupa setelah desain memberikan fx:id dan controllernya 
    • koneksi,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
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.koneksi;
    import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    /**
     *
     * @author herudi-pc
     */
    public class koneksi {
        private Connection con;
         
        public koneksi(){
        }
         
        public Connection connect(){
            if(con == null){
                MysqlDataSource db = new MysqlDataSource();
                db.setDatabaseName("biodata");
                db.setUser("root");
                db.setPassword("qwerty");
                try {
                    con = db.getConnection();
                } catch (SQLException e) {
                }
            }
            return con;
        }
         
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.koneksi;
    
    import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    /**
     *
     * @author herudi-pc
     */
    public class koneksi {
        private Connection con;
        
        public koneksi(){
        }
        
        public Connection connect(){
            if(con == null){
                MysqlDataSource db = new MysqlDataSource();
                db.setDatabaseName("biodata");
                db.setUser("root");
                db.setPassword("qwerty");
                try {
                    con = db.getConnection();
                } catch (SQLException e) {
                }
            }
            return con;
        }
        
    }
    
    
  • modelBiodata.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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.model;
     
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javafx.beans.property.ObjectProperty;
    import javafx.beans.property.SimpleObjectProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
     
    /**
     *
     * @author herudi-pc
     */
    public class modelBiodata {
        private final StringProperty id = new SimpleStringProperty();
        private final StringProperty nama = new SimpleStringProperty();
        private final StringProperty alamat = new SimpleStringProperty();
        private final ObjectProperty<Date> tanggalLahir = new SimpleObjectProperty<>();
        private String formatTanggal;
     
        public modelBiodata() {
        }
         
     
        public String getId() {
            return id.get();
        }
     
        public void setId(String value) {
            id.set(value);
        }
     
        public StringProperty idProperty() {
            return id;
        }
         
        public String getNama() {
            return nama.get();
        }
     
        public void setNama(String value) {
            nama.set(value);
        }
     
        public StringProperty namaProperty() {
            return nama;
        }
         
        public String getAlamat() {
            return alamat.get();
        }
     
        public void setAlamat(String value) {
            alamat.set(value);
        }
     
        public StringProperty alamatProperty() {
            return alamat;
        }
     
        public Date getTanggalLahir() {
            return tanggalLahir.get();
        }
     
        public void setTanggalLahir(Date value) {
            tanggalLahir.set(value);
        }
     
        public ObjectProperty tanggalLahirProperty() {
            return tanggalLahir;
        }
         
        public String getFormatTanggal() {
            Date tanggal = getTanggalLahir();
            SimpleDateFormat df = new SimpleDateFormat("dd-MMMM-yyyy");
            String format = df.format(tanggal);
            return format;
        }
     
        public void setFormatTanggal(String formatTanggal) {
            this.formatTanggal = formatTanggal;
        }
     
         
         
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.model;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javafx.beans.property.ObjectProperty;
    import javafx.beans.property.SimpleObjectProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    
    /**
     *
     * @author herudi-pc
     */
    public class modelBiodata {
        private final StringProperty id = new SimpleStringProperty();
        private final StringProperty nama = new SimpleStringProperty();
        private final StringProperty alamat = new SimpleStringProperty();
        private final ObjectProperty<Date> tanggalLahir = new SimpleObjectProperty<>();
        private String formatTanggal;
    
        public modelBiodata() {
        }
        
    
        public String getId() {
            return id.get();
        }
    
        public void setId(String value) {
            id.set(value);
        }
    
        public StringProperty idProperty() {
            return id;
        }
        
        public String getNama() {
            return nama.get();
        }
    
        public void setNama(String value) {
            nama.set(value);
        }
    
        public StringProperty namaProperty() {
            return nama;
        }
        
        public String getAlamat() {
            return alamat.get();
        }
    
        public void setAlamat(String value) {
            alamat.set(value);
        }
    
        public StringProperty alamatProperty() {
            return alamat;
        }
    
        public Date getTanggalLahir() {
            return tanggalLahir.get();
        }
    
        public void setTanggalLahir(Date value) {
            tanggalLahir.set(value);
        }
    
        public ObjectProperty tanggalLahirProperty() {
            return tanggalLahir;
        }
        
        public String getFormatTanggal() {
            Date tanggal = getTanggalLahir();
            SimpleDateFormat df = new SimpleDateFormat("dd-MMMM-yyyy");
            String format = df.format(tanggal);
            return format;
        }
    
        public void setFormatTanggal(String formatTanggal) {
            this.formatTanggal = formatTanggal;
        }
    
        
        
    }
    
  • interBiodata.java
  • 01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.interfaces;
     
    import biodata.model.modelBiodata;
    import javafx.collections.ObservableList;
     
    /**
     *
     * @author herudi-pc
     */
    public interface interBiodata {
        void insert(modelBiodata m);
        void delete(modelBiodata m);
        void update(modelBiodata m);
        ObservableList<modelBiodata> getAll();
        ObservableList<modelBiodata> likeByNama(String a);
        void autoId(modelBiodata m);
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.interfaces;
    
    import biodata.model.modelBiodata;
    import javafx.collections.ObservableList;
    
    /**
     *
     * @author herudi-pc
     */
    public interface interBiodata {
        void insert(modelBiodata m);
        void delete(modelBiodata m);
        void update(modelBiodata m);
        ObservableList<modelBiodata> getAll();
        ObservableList<modelBiodata> likeByNama(String a);
        void autoId(modelBiodata m);
    }
    
  • implBiodata.java
  • 001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    022
    023
    024
    025
    026
    027
    028
    029
    030
    031
    032
    033
    034
    035
    036
    037
    038
    039
    040
    041
    042
    043
    044
    045
    046
    047
    048
    049
    050
    051
    052
    053
    054
    055
    056
    057
    058
    059
    060
    061
    062
    063
    064
    065
    066
    067
    068
    069
    070
    071
    072
    073
    074
    075
    076
    077
    078
    079
    080
    081
    082
    083
    084
    085
    086
    087
    088
    089
    090
    091
    092
    093
    094
    095
    096
    097
    098
    099
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.implement;
     
    import biodata.interfaces.interBiodata;
    import biodata.koneksi.koneksi;
    import biodata.model.modelBiodata;
    import java.sql.Date;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
     
     
    public class implBiodata implements interBiodata {
        koneksi k;
     
        @Override
        public void insert(modelBiodata m) {
            k = new koneksi();
            PreparedStatement ps;
            try {
                ps = k.connect().prepareStatement("insert into tablebiodata values(?,?,?,?)");
                ps.setString(1, m.getId());
                ps.setString(2, m.getNama());
                ps.setString(3, m.getAlamat());
                ps.setDate(4, (Date) m.getTanggalLahir());
                ps.execute();
            } catch (Exception e) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, e);
            }
        }
     
        @Override
        public void delete(modelBiodata m) {
            k = new koneksi();
            PreparedStatement ps;
            try {
                ps = k.connect().prepareStatement("delete from tablebiodata where id = ?");
                ps.setString(1, m.getId());
                ps.execute();
            } catch (Exception e) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, e);
            }
        }
     
        @Override
        public void update(modelBiodata m) {
            k = new koneksi();
            PreparedStatement ps;
            try {
                ps = k.connect().prepareStatement("update tablebiodata set nama=?, alamat=?, tanggalLahir=? where id = ?");
                ps.setString(4, m.getId());
                ps.setString(1, m.getNama());
                ps.setString(2, m.getAlamat());
                ps.setDate(3, (Date) m.getTanggalLahir());
                ps.execute();
            } catch (Exception e) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, e);
            }
        }
     
        @Override
        public ObservableList<modelBiodata> getAll() {
            k = new koneksi();
            ObservableList<modelBiodata> listData = FXCollections.observableArrayList();
            try {
                String sql = "select * from tablebiodata";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {  
                    modelBiodata m = new modelBiodata();
                    m.setId(rs.getString(1));
                    m.setNama(rs.getString(2));
                    m.setAlamat(rs.getString(3));
                    m.setTanggalLahir(rs.getDate(4));
                    listData.add(m);
                }
            } catch (Exception ex) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, ex);
            }
            return listData;
        }
     
        @Override
        public ObservableList<modelBiodata> likeByNama(String a) {
            k = new koneksi();
            ObservableList<modelBiodata> listData = FXCollections.observableArrayList();
            try {
                String sql = "select * from tablebiodata where nama like '%"+a+"%'";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {  
                    modelBiodata m = new modelBiodata();
                    m.setId(rs.getString(1));
                    m.setNama(rs.getString(2));
                    m.setAlamat(rs.getString(3));
                    m.setTanggalLahir(rs.getDate(4));
                    listData.add(m);
                }
            } catch (Exception ex) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, ex);
            }
            return listData;
        }
     
        @Override
        public void autoId(modelBiodata m) {
            k = new koneksi();
            try {
                ResultSet rs = k.connect().createStatement().executeQuery("select * from tablebiodata");
                while(rs.next()){
                    String kode = rs.getString(1).substring(2);
                    String auto = ""+(Integer.parseInt(kode)+1);
                    String nol = "";
                    if (auto.length()==1) {
                        nol = "00";
                    }else if (auto.length()==2) {
                        nol = "0";
                    }else if (auto.length()==3) {
                        nol = "";
                    }
                    m.setId("B."+nol+auto);
                }
                if (m.getId()==null) {
                    m.setId("B.001");
                }
            } catch (SQLException ex) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
     
         
         
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.implement;
    
    import biodata.interfaces.interBiodata;
    import biodata.koneksi.koneksi;
    import biodata.model.modelBiodata;
    import java.sql.Date;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    
    
    public class implBiodata implements interBiodata {
        koneksi k;
    
        @Override
        public void insert(modelBiodata m) {
            k = new koneksi();
            PreparedStatement ps;
            try {
                ps = k.connect().prepareStatement("insert into tablebiodata values(?,?,?,?)");
                ps.setString(1, m.getId());
                ps.setString(2, m.getNama());
                ps.setString(3, m.getAlamat());
                ps.setDate(4, (Date) m.getTanggalLahir());
                ps.execute();
            } catch (Exception e) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, e);
            }
        }
    
        @Override
        public void delete(modelBiodata m) {
            k = new koneksi();
            PreparedStatement ps;
            try {
                ps = k.connect().prepareStatement("delete from tablebiodata where id = ?");
                ps.setString(1, m.getId());
                ps.execute();
            } catch (Exception e) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, e);
            }
        }
    
        @Override
        public void update(modelBiodata m) {
            k = new koneksi();
            PreparedStatement ps;
            try {
                ps = k.connect().prepareStatement("update tablebiodata set nama=?, alamat=?, tanggalLahir=? where id = ?");
                ps.setString(4, m.getId());
                ps.setString(1, m.getNama());
                ps.setString(2, m.getAlamat());
                ps.setDate(3, (Date) m.getTanggalLahir());
                ps.execute();
            } catch (Exception e) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, e);
            }
        }
    
        @Override
        public ObservableList<modelBiodata> getAll() {
            k = new koneksi();
            ObservableList<modelBiodata> listData = FXCollections.observableArrayList();
            try {
                String sql = "select * from tablebiodata";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {   
                    modelBiodata m = new modelBiodata();
                    m.setId(rs.getString(1));
                    m.setNama(rs.getString(2));
                    m.setAlamat(rs.getString(3));
                    m.setTanggalLahir(rs.getDate(4));
                    listData.add(m);
                }
            } catch (Exception ex) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, ex);
            }
            return listData;
        }
    
        @Override
        public ObservableList<modelBiodata> likeByNama(String a) {
            k = new koneksi();
            ObservableList<modelBiodata> listData = FXCollections.observableArrayList();
            try {
                String sql = "select * from tablebiodata where nama like '%"+a+"%'";
                ResultSet rs = k.connect().createStatement().executeQuery(sql);
                while (rs.next()) {   
                    modelBiodata m = new modelBiodata();
                    m.setId(rs.getString(1));
                    m.setNama(rs.getString(2));
                    m.setAlamat(rs.getString(3));
                    m.setTanggalLahir(rs.getDate(4));
                    listData.add(m);
                }
            } catch (Exception ex) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, ex);
            }
            return listData;
        }
    
        @Override
        public void autoId(modelBiodata m) {
            k = new koneksi();
            try {
                ResultSet rs = k.connect().createStatement().executeQuery("select * from tablebiodata");
                while(rs.next()){
                    String kode = rs.getString(1).substring(2);
                    String auto = ""+(Integer.parseInt(kode)+1);
                    String nol = "";
                    if (auto.length()==1) {
                        nol = "00";
                    }else if (auto.length()==2) {
                        nol = "0";
                    }else if (auto.length()==3) {
                        nol = "";
                    }
                    m.setId("B."+nol+auto);
                }
                if (m.getId()==null) {
                    m.setId("B.001");
                }
            } catch (SQLException ex) {
                Logger.getLogger(implBiodata.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        
        
    }
    
  • biodataController.java
  • 001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    022
    023
    024
    025
    026
    027
    028
    029
    030
    031
    032
    033
    034
    035
    036
    037
    038
    039
    040
    041
    042
    043
    044
    045
    046
    047
    048
    049
    050
    051
    052
    053
    054
    055
    056
    057
    058
    059
    060
    061
    062
    063
    064
    065
    066
    067
    068
    069
    070
    071
    072
    073
    074
    075
    076
    077
    078
    079
    080
    081
    082
    083
    084
    085
    086
    087
    088
    089
    090
    091
    092
    093
    094
    095
    096
    097
    098
    099
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.controller;
     
    import biodata.implement.implBiodata;
    import biodata.interfaces.interBiodata;
    import biodata.model.modelBiodata;
    import de.jensd.fx.fontawesome.AwesomeDude;
    import de.jensd.fx.fontawesome.AwesomeIcon;
    import java.net.URL;
    import java.sql.Date;
    import java.time.LocalDate;
    import java.util.ResourceBundle;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Alert;
    import javafx.scene.control.Button;
    import javafx.scene.control.DatePicker;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.TextArea;
    import javafx.scene.control.TextField;
    import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.scene.input.KeyEvent;
    import javafx.scene.input.MouseEvent;
    import javafx.stage.StageStyle;
     
    /**
     * FXML Controller class
     *
     * @author herudi-pc
     */
    public class biodataController implements Initializable {
        @FXML
        private TextField txtId;
        @FXML
        private TextField txtNama;
        @FXML
        private TextArea txtAlamat;
        @FXML
        private DatePicker dateTanggal;
        @FXML
        private Button btnSimpan;
        @FXML
        private Button btnHapus;
        @FXML
        private TableView<modelBiodata> tableData;
        @FXML
        private TableColumn<modelBiodata, String> colId;
        @FXML
        private TableColumn<modelBiodata, String> colNama;
        @FXML
        private TableColumn<modelBiodata, String> colAlamat;
        @FXML
        private TableColumn<modelBiodata, String> colTanggal;
        @FXML
        private TextField txtCari;
        @FXML
        private Button btnRefresh;
        interBiodata crudData = new implBiodata();
        ObservableList<modelBiodata> listData;
        private String StatusKode;
        /**
         * Initializes the controller class.
         * @param url
         * @param rb
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            colId.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelBiodata, String> cellData) ->
                            cellData.getValue().idProperty());
            colNama.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelBiodata, String> cellData) ->
                            cellData.getValue().namaProperty());
            colAlamat.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelBiodata, String> cellData) ->
                            cellData.getValue().alamatProperty());
            colTanggal.setCellValueFactory(new PropertyValueFactory("formatTanggal"));
            listData = FXCollections.observableArrayList();
            AwesomeDude.setIcon(btnSimpan, AwesomeIcon.CHECK_SQUARE, "15px");
            AwesomeDude.setIcon(btnRefresh, AwesomeIcon.CHAIN_BROKEN, "15px");
            AwesomeDude.setIcon(btnHapus, AwesomeIcon.ERASER, "15px");
            StatusKode = "0";
            tampilData();
            autoId();
            tableData.getSelectionModel().clearSelection();
            // TODO
        }
         
        private void dialog(Alert.AlertType alertType,String s){
            Alert alert = new Alert(alertType,s);
            alert.initStyle(StageStyle.UTILITY);
            alert.setTitle("Info");
            alert.showAndWait();
        }
         
        private void clear(){
            txtId.clear();
            txtNama.clear();
            txtAlamat.clear();
            txtCari.clear();
            dateTanggal.setValue(null);
            StatusKode = "0";
        }
         
        private void tampilData(){
            listData = crudData.getAll();
            tableData.setItems(listData);
        }
         
        private void autoId(){
            modelBiodata m = new modelBiodata();
            crudData.autoId(m);
            txtId.setText(m.getId());
        }
     
        @FXML
        private void aksiSimpan(ActionEvent event) {
            modelBiodata m = new modelBiodata();
            m.setId(txtId.getText());
            m.setNama(txtNama.getText());
            m.setAlamat(txtAlamat.getText());
            m.setTanggalLahir(Date.valueOf(dateTanggal.getValue()));
            if (StatusKode.equals("0")) {
                crudData.insert(m);
            }else{
                crudData.update(m);
            }
            dialog(Alert.AlertType.INFORMATION, "Data Telah Tersimpan");
            tampilData();
            clear();
            autoId();
             
        }
     
        @FXML
        private void aksiHapus(ActionEvent event) {
            modelBiodata m = new modelBiodata();
            m.setId(txtId.getText());
            crudData.delete(m);
            dialog(Alert.AlertType.INFORMATION, "Data Berhasil Dihapus");
            tampilData();
            clear();
        }
     
        @FXML
        private void klikTableData(MouseEvent event) {
            StatusKode = "1";
            try {
                modelBiodata klik = tableData.getSelectionModel().getSelectedItems().get(0);
                txtId.setText(klik.getId());
                txtNama.setText(klik.getNama());
                txtAlamat.setText(klik.getAlamat());
                dateTanggal.setValue(LocalDate.parse(klik.getTanggalLahir().toString()));
            } catch (Exception e) {
            }
        }
     
        @FXML
        private void aksiCari(KeyEvent event) {
            listData = crudData.likeByNama(txtCari.getText());
            tableData.setItems(listData);
        }
     
        @FXML
        private void aksiRefresh(ActionEvent event) {
            clear();
            tampilData();
            autoId();
        }
         
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata.controller;
    
    import biodata.implement.implBiodata;
    import biodata.interfaces.interBiodata;
    import biodata.model.modelBiodata;
    import de.jensd.fx.fontawesome.AwesomeDude;
    import de.jensd.fx.fontawesome.AwesomeIcon;
    import java.net.URL;
    import java.sql.Date;
    import java.time.LocalDate;
    import java.util.ResourceBundle;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Alert;
    import javafx.scene.control.Button;
    import javafx.scene.control.DatePicker;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.TextArea;
    import javafx.scene.control.TextField;
    import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.scene.input.KeyEvent;
    import javafx.scene.input.MouseEvent;
    import javafx.stage.StageStyle;
    
    /**
     * FXML Controller class
     *
     * @author herudi-pc
     */
    public class biodataController implements Initializable {
        @FXML
        private TextField txtId;
        @FXML
        private TextField txtNama;
        @FXML
        private TextArea txtAlamat;
        @FXML
        private DatePicker dateTanggal;
        @FXML
        private Button btnSimpan;
        @FXML
        private Button btnHapus;
        @FXML
        private TableView<modelBiodata> tableData;
        @FXML
        private TableColumn<modelBiodata, String> colId;
        @FXML
        private TableColumn<modelBiodata, String> colNama;
        @FXML
        private TableColumn<modelBiodata, String> colAlamat;
        @FXML
        private TableColumn<modelBiodata, String> colTanggal;
        @FXML
        private TextField txtCari;
        @FXML
        private Button btnRefresh;
        interBiodata crudData = new implBiodata();
        ObservableList<modelBiodata> listData;
        private String StatusKode;
        /**
         * Initializes the controller class.
         * @param url
         * @param rb
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            colId.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelBiodata, String> cellData) ->
                            cellData.getValue().idProperty());
            colNama.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelBiodata, String> cellData) ->
                            cellData.getValue().namaProperty());
            colAlamat.setCellValueFactory(
                    (TableColumn.CellDataFeatures<modelBiodata, String> cellData) ->
                            cellData.getValue().alamatProperty());
            colTanggal.setCellValueFactory(new PropertyValueFactory("formatTanggal"));
            listData = FXCollections.observableArrayList();
            AwesomeDude.setIcon(btnSimpan, AwesomeIcon.CHECK_SQUARE, "15px");
            AwesomeDude.setIcon(btnRefresh, AwesomeIcon.CHAIN_BROKEN, "15px");
            AwesomeDude.setIcon(btnHapus, AwesomeIcon.ERASER, "15px");
            StatusKode = "0";
            tampilData();
            autoId();
            tableData.getSelectionModel().clearSelection();
            // TODO
        } 
        
        private void dialog(Alert.AlertType alertType,String s){
            Alert alert = new Alert(alertType,s);
            alert.initStyle(StageStyle.UTILITY);
            alert.setTitle("Info");
            alert.showAndWait();
        }
        
        private void clear(){
            txtId.clear();
            txtNama.clear();
            txtAlamat.clear();
            txtCari.clear();
            dateTanggal.setValue(null);
            StatusKode = "0";
        }
        
        private void tampilData(){
            listData = crudData.getAll();
            tableData.setItems(listData);
        }
        
        private void autoId(){
            modelBiodata m = new modelBiodata();
            crudData.autoId(m);
            txtId.setText(m.getId());
        }
    
        @FXML
        private void aksiSimpan(ActionEvent event) {
            modelBiodata m = new modelBiodata();
            m.setId(txtId.getText());
            m.setNama(txtNama.getText());
            m.setAlamat(txtAlamat.getText());
            m.setTanggalLahir(Date.valueOf(dateTanggal.getValue()));
            if (StatusKode.equals("0")) {
                crudData.insert(m);
            }else{
                crudData.update(m);
            }
            dialog(Alert.AlertType.INFORMATION, "Data Telah Tersimpan");
            tampilData();
            clear();
            autoId();
            
        }
    
        @FXML
        private void aksiHapus(ActionEvent event) {
            modelBiodata m = new modelBiodata();
            m.setId(txtId.getText());
            crudData.delete(m);
            dialog(Alert.AlertType.INFORMATION, "Data Berhasil Dihapus");
            tampilData();
            clear();
        }
    
        @FXML
        private void klikTableData(MouseEvent event) {
            StatusKode = "1";
            try {
                modelBiodata klik = tableData.getSelectionModel().getSelectedItems().get(0);
                txtId.setText(klik.getId());
                txtNama.setText(klik.getNama());
                txtAlamat.setText(klik.getAlamat());
                dateTanggal.setValue(LocalDate.parse(klik.getTanggalLahir().toString()));
            } catch (Exception e) {
            }
        }
    
        @FXML
        private void aksiCari(KeyEvent event) {
            listData = crudData.likeByNama(txtCari.getText());
            tableData.setItems(listData);
        }
    
        @FXML
        private void aksiRefresh(ActionEvent event) {
            clear();
            tampilData();
            autoId();
        }
        
    }
    
  • biodataCss.css
  • 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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    /*
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    */
    /*
        Created on : Nov 6, 2014, 10:54:20 PM
        Author     : herudi-pc
    */
     
    .headerPane{
        -fx-background-color:  derive(grey, -30%);
    }
     
    .table-view {
        -fx-base: white;
        -fx-control-inner-background: white;
        -fx-background-color: #c7c7c7;
        -fx-table-cell-border-color: transparent;
        -fx-table-header-border-color: transparent;
        -fx-padding: 2;
    }
     
    .table-view .column-header-background {
        -fx-background-color: #42539d;
    }
     
    .table-view .column-header, .table-view .filler {
        -fx-size: 35;
        -fx-border-width: 0 0 0 0;
        -fx-background-color: transparent;
        -fx-border-color:
            transparent
            transparent
            derive(-fx-base, 80%)
            transparent;
        -fx-border-insets: 0 10 1 0;
    }
     
    .table-view .column-header .label {
        -fx-font-size: 12px;
        -fx-font-family: "Segoe UI bold";
        -fx-text-fill: white;
        -fx-alignment: center-left;
    }
     
    .table-view:focused .table-row-cell:filled:focused:selected {
        -fx-background-color: -fx-focus-color;
    }
     
    .table-cell {    
        -fx-cell-size: 4.0em;
        -fx-padding: 1em 0em 0.1em 0.1em;
        -fx-font: 12px "Segoe UI";   
        -fx-alignment: bottom-left;
    }
     
    .table-row-cell:empty {
        -fx-background-color: #f8f8f8;
        -fx-base: transparent;
        -fx-control-inner-background: transparent;
        -fx-table-cell-border-color: transparent;
        -fx-table-header-border-color: transparent;
        -fx-padding: 0;
    }
       
    .table-row-cell:empty .table-cell {
        -fx-border-width: 0px;
    }
    /*
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    */
    /* 
        Created on : Nov 6, 2014, 10:54:20 PM
        Author     : herudi-pc
    */
    
    .headerPane{
        -fx-background-color:  derive(grey, -30%);
    }
    
    .table-view {
        -fx-base: white;
        -fx-control-inner-background: white;
        -fx-background-color: #c7c7c7;
        -fx-table-cell-border-color: transparent;
        -fx-table-header-border-color: transparent;
        -fx-padding: 2;
    }
    
    .table-view .column-header-background {
        -fx-background-color: #42539d;
    }
    
    .table-view .column-header, .table-view .filler {
        -fx-size: 35;
        -fx-border-width: 0 0 0 0;
        -fx-background-color: transparent;
        -fx-border-color: 
            transparent
            transparent
            derive(-fx-base, 80%) 
            transparent;
        -fx-border-insets: 0 10 1 0;
    }
    
    .table-view .column-header .label {
        -fx-font-size: 12px;
        -fx-font-family: "Segoe UI bold";
        -fx-text-fill: white;
        -fx-alignment: center-left;
    }
    
    .table-view:focused .table-row-cell:filled:focused:selected {
        -fx-background-color: -fx-focus-color;
    }
    
    .table-cell {     
        -fx-cell-size: 4.0em;
        -fx-padding: 1em 0em 0.1em 0.1em;
        -fx-font: 12px "Segoe UI";    
        -fx-alignment: bottom-left;
    }
    
    .table-row-cell:empty {
        -fx-background-color: #f8f8f8;
        -fx-base: transparent;
        -fx-control-inner-background: transparent;
        -fx-table-cell-border-color: transparent;
        -fx-table-header-border-color: transparent;
        -fx-padding: 0;
    }
      
    .table-row-cell:empty .table-cell {
        -fx-border-width: 0px;
    }
    
  • Biodata.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
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata;
     
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
     
    /**
     *
     * @author herudi-pc
     */
    public class Biodata extends Application {
         
        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("/biodata/view/biodata.fxml"));
             
            Scene scene = new Scene(root);
             
            stage.setScene(scene);
            stage.setTitle("Biodata");
            stage.show();
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
         
    }
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package biodata;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    /**
     *
     * @author herudi-pc
     */
    public class Biodata extends Application {
        
        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("/biodata/view/biodata.fxml"));
            
            Scene scene = new Scene(root);
            
            stage.setScene(scene);
            stage.setTitle("Biodata");
            stage.show();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
        
    }
    
    ane kira sudah jelas tutornya semua source kode telah ane sisipkan diatas. mengenai desain di scene builder silahkan perdalam lagi, atau bila perlu tanya ke mbah google dan bisa juga bertanya di kolom komentar. Happy Coding . .