Pada saat ini saya akan membuat tampilan form pengadaan
Setelah membuat interface seperti di atas langkah selanjutnya membuat kunci koneksi dan mendeklarasikan kunci koneksi
package database;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import database_koneksi.koneksi;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
*
*/
public class formpengadaanbarang extends javax.swing.JFrame {
koneksi koneksi;
ResultSet resultSet;
Statement statement;
koneksi con;
int count = 100;
String id_pengadaan;
String id_hapus;
public formpengadaanbarang() {
initComponents();
koneksi = new koneksi();
tampilanpengadaan();
}
private void tampilanpengadaan() {
Object header[] = {"ID PENGADAAN", "TANGGAL", "SUPPLIER", "TOTAL", "ACTION"};
DefaultTableModel defaultTableModel = new DefaultTableModel(null, header);
tabelPengadaan.setModel(defaultTableModel);
//menghapus tabel sebelum menampilkan data
int baris = tabelPengadaan.getRowCount();
for (int i = 0; i < baris; i++) {
defaultTableModel.removeRow(i);
}
String sql = "select * from pengadaan";
try {
statement = koneksi.connection.createStatement();
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
String kolom1 = resultSet.getString(1);
String kolom2 = resultSet.getString(3);
String kolom3 = resultSet.getString(4);
String kolom4 = resultSet.getString(5);
String kolom[] = {kolom1, kolom2, kolom3, kolom4, ""};
defaultTableModel.addRow(kolom);
tabelPengadaan.getColumnModel().getColumn(4).setCellRenderer(new buttondetil());
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
void insertpengadaan() {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String tanggal = dateFormat.format(txttanggal.getDate());
String sqltanggal = "select TANGGAL,ID_PENGADAAN from pengadaan";
resultSet = statement.executeQuery(sqltanggal);
while (resultSet.next()) {
String data1 = resultSet.getString(1);
String data2 = resultSet.getString(2);
if (data1.equals(tanggal)) {
if (count == Integer.valueOf(data2.substring(14, 17))) {
count++;
}
}
}
String idpengadaan = "TB." + tanggal + "." + count;
String sql = "insert into pengadaan values('" + idpengadaan + "','fedra','" + tanggal + "','" + txtsupplier.getText() + "',0)";
statement.executeUpdate(sql);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
private void Hapuspengadaan() {
String query = "delete from pengadaan where id_pengadaan='" + id_hapus + "'";
try {
statement.executeUpdate(query);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "data dipakai ");
}
}
private void clear() {
txtsupplier.setText("");
txttanggal.setDate(null);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
txttanggal = new com.toedter.calendar.JDateChooser();
txtsupplier = new javax.swing.JTextField();
jButtoncancel = new javax.swing.JButton();
jButtondel = new javax.swing.JButton();
jButtoninsert = new javax.swing.JButton();
lblTanggal = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
tabelPengadaan = new javax.swing.JTable();
jButtoncancel1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jLabel1.setFont(new java.awt.Font("Calibri", 1, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(0, 0, 102));
jLabel1.setText("FORM TRANSAKSI PENGADAAN BARANG");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
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)
.addGap(147, 147, 147))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabel1)
.addContainerGap(26, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jLabel4.setText("Tanggal");
jLabel5.setText("Supplier");
txttanggal.setDateFormatString("dd-MM-yyyy");
txtsupplier.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
txtsupplierKeyPressed(evt);
}
});
jButtoncancel.setText("Back");
jButtoncancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtoncancelActionPerformed(evt);
}
});
jButtondel.setText("Delete");
jButtondel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtondelActionPerformed(evt);
}
});
jButtoninsert.setText("Insert");
jButtoninsert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtoninsertActionPerformed(evt);
}
});
tabelPengadaan.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"
}
));
tabelPengadaan.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tabelPengadaanMouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
tabelPengadaanMouseEntered(evt);
}
});
jScrollPane1.setViewportView(tabelPengadaan);
jButtoncancel1.setText("Cancel");
jButtoncancel1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtoncancel1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(42, 42, 42)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txttanggal, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
.addComponent(txtsupplier))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblTanggal, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(288, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 658, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
.addComponent(jButtoncancel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtoncancel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtondel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtoninsert)))
.addGap(30, 30, 30))))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(lblTanggal, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txttanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtsupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtoncancel)
.addComponent(jButtondel)
.addComponent(jButtoninsert)
.addComponent(jButtoncancel1))
.addGap(447, 447, 447))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
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)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-718)/2, (screenSize.height-623)/2, 718, 623);
}// </editor-fold>
private void jButtoninsertActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (txtsupplier.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "Pengisian Form Harus Lengkap");
} else {
insertpengadaan();
tampilanpengadaan();
clear();
}
}
private void jButtondelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (txtsupplier.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "Pilih data yang akan dihapus");
}else {
int hasil = JOptionPane.showConfirmDialog(null, "Apakah benar anda ingin menghapus kode kategori : "
+id_hapus,null,JOptionPane.YES_NO_OPTION);
if (hasil == JOptionPane.YES_OPTION) {
Hapuspengadaan();
tampilanpengadaan();
clear();
}else if (hasil == JOptionPane.NO_OPTION) {
clear();
}
}
}
private void tabelPengadaanMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
id_pengadaan= tabelPengadaan.getValueAt(tabelPengadaan.getSelectedRow(),0).toString();
if (tabelPengadaan.getSelectedColumn() == 4) {
this.dispose();
new form_detail_transaksi(id_pengadaan).setVisible(true);
}
id_hapus = tabelPengadaan.getValueAt(tabelPengadaan.getSelectedRow(), 0).toString();
txtsupplier.setText(tabelPengadaan.getValueAt(tabelPengadaan.getSelectedRow(), 2).toString());
}
private void txtsupplierKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if (evt.getKeyChar() == '\n') {
insertpengadaan();
tampilanpengadaan();
clear();
}
}
private void jButtoncancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new Menu_Utama(Form_Login.kondisiLogin).setVisible(true);
this.dispose();
}
private void tabelPengadaanMouseEntered(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void jButtoncancel1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
clear();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(formpengadaanbarang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(formpengadaanbarang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(formpengadaanbarang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(formpengadaanbarang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new formpengadaanbarang().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtoncancel;
private javax.swing.JButton jButtoncancel1;
private javax.swing.JButton jButtondel;
private javax.swing.JButton jButtoninsert;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lblTanggal;
private javax.swing.JTable tabelPengadaan;
private javax.swing.JTextField txtsupplier;
private com.toedter.calendar.JDateChooser txttanggal;
// End of variables declaration
}