Advertisement
gurumutant

Form Anggota setengah jadi

Aug 23rd, 2019
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.46 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package cobajavaswing;
  7.  
  8. import com.mysql.jdbc.Statement;
  9. import java.sql.Connection;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import javax.swing.table.DefaultTableModel;
  13.  
  14. /**
  15.  *
  16.  * @author gurum
  17.  */
  18. public class frmAnggota extends javax.swing.JFrame {
  19.      DefaultTableModel tableModel;
  20.      private Connection conn;
  21.     /**
  22.      * Creates new form frmAnggota
  23.      */
  24.     public frmAnggota() {
  25.         initComponents();
  26.         initTable();
  27.         loadData();
  28.        
  29.     }
  30.  
  31.     /**
  32.      * This method is called from within the constructor to initialize the form.
  33.      * WARNING: Do NOT modify this code. The content of this method is always
  34.      * regenerated by the Form Editor.
  35.      */
  36.     @SuppressWarnings("unchecked")
  37.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  38.     private void initComponents() {
  39.  
  40.         jLabel1 = new javax.swing.JLabel();
  41.         jScrollPane1 = new javax.swing.JScrollPane();
  42.         jtblAnggota = new javax.swing.JTable();
  43.         btnRefresh = new javax.swing.JButton();
  44.  
  45.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  46.  
  47.         jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
  48.         jLabel1.setText("Form Anggota");
  49.  
  50.         jtblAnggota.setModel(new javax.swing.table.DefaultTableModel(
  51.             new Object [][] {
  52.                 {null, null, null, null},
  53.                 {null, null, null, null},
  54.                 {null, null, null, null},
  55.                 {null, null, null, null}
  56.             },
  57.             new String [] {
  58.                 "Title 1", "Title 2", "Title 3", "Title 4"
  59.             }
  60.         ));
  61.         jScrollPane1.setViewportView(jtblAnggota);
  62.  
  63.         btnRefresh.setText("Refresh");
  64.         btnRefresh.addActionListener(new java.awt.event.ActionListener() {
  65.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  66.                 btnRefreshActionPerformed(evt);
  67.             }
  68.         });
  69.  
  70.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  71.         getContentPane().setLayout(layout);
  72.         layout.setHorizontalGroup(
  73.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  74.             .addGroup(layout.createSequentialGroup()
  75.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  76.                     .addComponent(btnRefresh)
  77.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  78.                         .addGroup(layout.createSequentialGroup()
  79.                             .addContainerGap()
  80.                             .addComponent(jLabel1))
  81.                         .addGroup(layout.createSequentialGroup()
  82.                             .addGap(35, 35, 35)
  83.                             .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE))))
  84.                 .addContainerGap(47, Short.MAX_VALUE))
  85.         );
  86.         layout.setVerticalGroup(
  87.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  88.             .addGroup(layout.createSequentialGroup()
  89.                 .addContainerGap()
  90.                 .addComponent(jLabel1)
  91.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 170, Short.MAX_VALUE)
  92.                 .addComponent(btnRefresh)
  93.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  94.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
  95.                 .addContainerGap())
  96.         );
  97.  
  98.         pack();
  99.     }// </editor-fold>                        
  100.  
  101.     private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {                                          
  102.         loadData();
  103.     }                                          
  104.  
  105.     /**
  106.      * @param args the command line arguments
  107.      */
  108.     public static void main(String args[]) {
  109.         /* Set the Nimbus look and feel */
  110.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  111.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  112.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  113.          */
  114.         try {
  115.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  116.                 if ("Nimbus".equals(info.getName())) {
  117.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  118.                     break;
  119.                 }
  120.             }
  121.         } catch (ClassNotFoundException ex) {
  122.             java.util.logging.Logger.getLogger(frmAnggota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  123.         } catch (InstantiationException ex) {
  124.             java.util.logging.Logger.getLogger(frmAnggota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  125.         } catch (IllegalAccessException ex) {
  126.             java.util.logging.Logger.getLogger(frmAnggota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  127.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  128.             java.util.logging.Logger.getLogger(frmAnggota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  129.         }
  130.         //</editor-fold>
  131.  
  132.         /* Create and display the form */
  133.         java.awt.EventQueue.invokeLater(new Runnable() {
  134.             public void run() {
  135.                 new frmAnggota().setVisible(true);
  136.             }
  137.         });
  138.     }
  139.  
  140.     // Variables declaration - do not modify                    
  141.     private javax.swing.JButton btnRefresh;
  142.     private javax.swing.JLabel jLabel1;
  143.     private javax.swing.JScrollPane jScrollPane1;
  144.     private javax.swing.JTable jtblAnggota;
  145.     // End of variables declaration                  
  146.  
  147.     private void initTable() {
  148.         tableModel = new DefaultTableModel();
  149.         jtblAnggota.setModel(tableModel);
  150.         tableModel.addColumn("ID");
  151.         tableModel.addColumn("Nama Anggota");
  152.         tableModel.addColumn("No HP");
  153.         tableModel.addColumn("Alamat");
  154.     }
  155.  
  156. private void loadData(){
  157.         //menghapus seluruh data yg ada di tabel
  158.         tableModel.getDataVector().removeAllElements();
  159.         //pemberitahuan tabel telah kosong
  160.         tableModel.fireTableDataChanged();
  161.        
  162.         try{
  163.             conn = Koneksi.getConnection();
  164.             String query = "SELECT * FROM anggota";
  165.            
  166.             Statement stmt = (Statement) conn.createStatement();
  167.             ResultSet rs = stmt.executeQuery(query);
  168.            
  169.             //mengisi tabel dgn data hasil query
  170.             while (rs.next()){
  171.                 Object[] anggota = new Object[6];
  172.                 anggota[0] = rs.getString("Kode_Anggota");
  173.                 anggota[1] = rs.getString("Nama_Anggota");
  174.                 anggota[2] = rs.getString("no_HP");
  175.                 anggota[3] = rs.getString("alamat_anggota");
  176.                 tableModel.addRow(anggota);
  177.             }
  178.             rs.close();
  179.             stmt.close();
  180.         } catch (SQLException ex) {
  181.             System.out.println(ex.getMessage());
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement