Java tutorial
/* * 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 MstrPetugas; import Class.DbKoneksi; import Class.MstrPetugas; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.text.SimpleDateFormat; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import org.apache.commons.lang.StringUtils; public class FormPetugas extends javax.swing.JFrame { Connection _Cnn; DbKoneksi getCnn = new DbKoneksi(); private Dimension dimensi = Toolkit.getDefaultToolkit().getScreenSize(); private DefaultTableModel _Tampung_tabel_mstr_petugas; public FormPetugas() { initComponents(); this.setLocation(dimensi.width / 2 - this.getWidth() / 2, dimensi.height / 2 - this.getHeight() / 2); String[] kolom = { "Id", "Nama ", "Telepon" }; _Tampung_tabel_mstr_petugas = new DefaultTableModel(null, kolom) { Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.String.class }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } // Agar table tidak bisa diedit public boolean isCellEditable(int row, int col) { int cola = _Tampung_tabel_mstr_petugas.getColumnCount(); return (col < cola) ? false : true; } }; tabel_petugas.setModel(_Tampung_tabel_mstr_petugas); LoadData(); } public void HapusTabel() { int row = _Tampung_tabel_mstr_petugas.getRowCount(); for (int i = 0; i < row; i++) { _Tampung_tabel_mstr_petugas.removeRow(0); } } private void LoadData() { try { _Cnn = null; DbKoneksi getCnn = new DbKoneksi(); _Cnn = getCnn.getConnection(); HapusTabel(); String sql = "" + " SELECT id_petugas,nama,no_telepon" + " FROM petugas"; Statement stat = _Cnn.createStatement(); ResultSet res = stat.executeQuery(sql); while (res.next()) { String id_petugas = res.getString(1); String nama = res.getString(2); String status = res.getString(3); Object[] data = { id_petugas, nama, status }; _Tampung_tabel_mstr_petugas.addRow(data); } tabel_petugas.getColumnModel().getColumn(0).setPreferredWidth(10); tabel_petugas.getColumnModel().getColumn(1).setPreferredWidth(50); tabel_petugas.getColumnModel().getColumn(2).setPreferredWidth(50); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Error : " + ex); } } ActionListener actionListener; private javax.swing.Timer timer = new javax.swing.Timer(100, actionListener); private void Refresh() { try { ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { LoadData(); timer.stop(); } }; timer = new javax.swing.Timer(100, al); timer.start(); } catch (Exception ex) { } } private void Search(String _ID) { try { MstrPetugas x = new MstrPetugas(); x.Search(_ID); if (x._Akses.equals("-")) { txt_id.setText(x.id_petugas); txt_nama.setText(x.nama); txt_no_telepon.setText(x.no_telepon); txt_password.setText(x.password); txt_id.setEditable(false); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex); } } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); txt_password = new javax.swing.JPasswordField(); txt_id = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); tabel_petugas = new javax.swing.JTable(); txt_nama = new javax.swing.JTextField(); jButton4 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); btn_simpan = new javax.swing.JButton(); txt_no_telepon = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jPanel1.setBackground(new java.awt.Color(0, 123, 255)); jLabel1.setFont(new java.awt.Font("FrankRuehl", 1, 24)); // NOI18N jLabel1.setForeground(new java.awt.Color(255, 255, 255)); jLabel1.setText("Data Petugas"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(148, 148, 148).addComponent(jLabel1) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1).addContainerGap())); jLabel2.setText("Id "); jLabel3.setText("nama"); jLabel4.setText("No Telepon"); jLabel5.setText("Password"); tabel_petugas .setModel(new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); jScrollPane1.setViewportView(tabel_petugas); jButton4.setText("Hapus"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton3.setText("Batal"); jButton2.setText("Edit"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); btn_simpan.setText("Simpan"); btn_simpan.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_simpanActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txt_id, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txt_nama, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE) .addComponent(txt_password, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE) .addComponent(txt_no_telepon)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addComponent(btn_simpan, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(89, 89, 89)))) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(23, 23, 23) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txt_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(txt_nama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(4, 4, 4) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5).addComponent(txt_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(9, 9, 9) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(txt_no_telepon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(28, 28, 28) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btn_simpan).addComponent(jButton4).addComponent(jButton2) .addComponent(jButton3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void btn_simpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_simpanActionPerformed try { MstrPetugas x = new MstrPetugas(); x.Search(txt_id.getText()); x.id_petugas = txt_id.getText(); x.nama = txt_nama.getText(); x.no_telepon = txt_no_telepon.getText(); x.password = txt_password.getText(); if (x._Akses.equals("-")) { x.UpdateData(); txt_id.setEditable(false); JOptionPane.showMessageDialog(null, "Data Berhasil diubah"); Refresh(); } else { x.SaveData(); txt_id.setEditable(false); JOptionPane.showMessageDialog(null, "Data Berhasil disimpan"); Refresh(); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex); } }//GEN-LAST:event_btn_simpanActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed try { String kode = tabel_petugas.getValueAt(tabel_petugas.getSelectedRow(), 0).toString(); Search(kode); Refresh(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, ex); } }//GEN-LAST:event_jButton2ActionPerformed private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed String kode = tabel_petugas.getValueAt(tabel_petugas.getSelectedRow(), 0).toString(); int jawab = JOptionPane.showConfirmDialog(this, "Hapus ? Data Petugas : " + kode, "Konfirmasi", JOptionPane.YES_NO_OPTION); if (jawab == JOptionPane.YES_OPTION) { MstrPetugas x = new MstrPetugas(); x.Delete(kode); LoadData(); } }//GEN-LAST:event_jButton4ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btn_simpan; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable tabel_petugas; private javax.swing.JTextField txt_id; private javax.swing.JTextField txt_nama; private javax.swing.JTextField txt_no_telepon; private javax.swing.JPasswordField txt_password; // End of variables declaration//GEN-END:variables }