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 . .
    9 komentar :

    9 komentar :

    1. //di kodenya ditambang komentar tentang kodenya dong master ...

      BalasHapus
    2. ini pakai JDK 8 Update berapa gan?

      BalasHapus
    3. Tambahin lagi donk gan tutorial tentang Scene Buildernya, soalnya ane nubie banget nih :)

      BalasHapus
      Balasan
      1. insya allah ntr ditambah gan. . .

        Hapus
    4. Mas untuk yang daftar baju, saya coba eror, di login user saya coba dalam combo box kosong juga kosong, tetap eror, kalau saya lihat dalam source code harusnya keluar jendela dialog, ini tidak sama sekali berbeda dengan biodata dimana erornya saya cek memberitahu belum connect, kalau daftar harga baju keluar erornya seperti ini :

      Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
      ....................................................................

      sebenarnya banyak dibawahnya, tapi setahu saya baris pertama dan kedua yang penting, semoga ada pencerahanya. Terima kasih saya sudah belajar dari Mas Rudi, tapi saya penasaran untuk perpindahan scene terjadi eror umpama saya buat frame dengan menu bar dengan menu item informasi, waktu saya tekan pilihan menu informasi keluar eror yang sama seperti yang terjadi pada daftar harga baju.

      BalasHapus
    5. Selamat pagi Mas Bisa kasih Contoh Input Data Ke Database Dari tableView, Bukan Dari TexField


      Terima Kasih

      BalasHapus
    6. Selamat pagi Mas Bisa kasih Contoh Input Data Ke Database Dari tableView, Bukan Dari TexField


      Terima Kasih

      BalasHapus