package aplikasiBarang;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
import java.util.*;
import java.io.*;
/**
*
* @author deLL
*/
public class aplikasiBarang extends JFrame implements ActionListener{
//deklarasi variabel
JLabel ljudul=new JLabel("::Data Barang::");
JLabel lkode=new JLabel("Kode Barang");
JLabel lnama=new JLabel("Nama Barang");
JLabel ltgl=new JLabel("Tanggal Beli");
JLabel lspek=new JLabel("Spesifikasi");
JLabel lharga=new JLabel("Harga Barang");
JLabel lstok=new JLabel("Stok Barang");
JLabel ltabel=new JLabel("::Daftar Data Barang");
JTextField tkode=new JTextField();
JTextField txtkode=new JTextField();
JTextField tnama=new JTextField();
JTextField tharga=new JTextField();
JTextField tstok=new JTextField();
JTextField tcari=new JTextField();
JComboBox ttglh=new JComboBox();
JComboBox ttglb=new JComboBox();
JComboBox ttglt=new JComboBox();
JComboBox tkolomcari=new JComboBox();
JButton btambah=new JButton("tambah");
JButton bsimpan=new JButton("simpan");
JButton bbatal=new JButton("batal");
JButton bubah=new JButton("ubah");
JButton bhapus=new JButton("hapus");
JButton bcari=new JButton("cari");
JButton btutup=new JButton("tutup");
JButton bnext=new JButton(">>");
JButton bprev=new JButton("<<");
JTextArea tspek=new JTextArea();
JPanel ptombol=new JPanel();
JTable tblbarang=new JTable();
JScrollPane scroll=new JScrollPane();
DefaultTableModel tabMode;
boolean simpan=false;
int n;
public aplikasiBarang(){
ljudul.setBounds(new Rectangle(150,5,200,20));
lkode.setBounds(new Rectangle(70,40,80,20));
lnama.setBounds(new Rectangle(70,65,80,20));
ltgl.setBounds(new Rectangle(70,90,90,20));
lspek.setBounds(new Rectangle(70,115,80,20));
lharga.setBounds(new Rectangle(70,160,80,20));
lstok.setBounds(new Rectangle(70,185,80,20));
tkode.setBounds(new Rectangle(170,40,60,20));
txtkode.setBounds(new Rectangle(170,40,80,20));
tnama.setBounds(new Rectangle(170,65,190,20));
ttglh.setBounds(new Rectangle(170,90,40,20));
ttglb.setBounds(new Rectangle(215,90,85,20));
ttglt.setBounds(new Rectangle(305,90,55,20));
tspek.setBounds(new Rectangle(170,115,190,40));
tharga.setBounds(new Rectangle(170,160,100,20));
tstok.setBounds(new Rectangle(170,185,50,20));
btambah.setBounds(new Rectangle(10,220,80,20));
bsimpan.setBounds(new Rectangle(100,220,80,20));
bbatal.setBounds(new Rectangle(190,220,70,20));
bubah.setBounds(new Rectangle(270,220,70,20));
bhapus.setBounds(new Rectangle(350,220,70,20));
btambah.setBounds(new Rectangle(10,220,80,20));
bsimpan.setBounds(new Rectangle(100,220,80,20));
bbatal.setBounds(new Rectangle(190,220,70,20));
bubah.setBounds(new Rectangle(270,220,70,20));
bhapus.setBounds(new Rectangle(350,220,70,20));
btambah.setBounds(new Rectangle(10,220,80,20));
bsimpan.setBounds(new Rectangle(100,220,80,20));
bbatal.setBounds(new Rectangle(190,220,70,20));
bubah.setBounds(new Rectangle(270,220,70,20));
bhapus.setBounds(new Rectangle(350,220,70,20));
btambah.setBounds(new Rectangle(10,220,80,20));
bsimpan.setBounds(new Rectangle(100,220,80,20));
bbatal.setBounds(new Rectangle(190,220,70,20));
bubah.setBounds(new Rectangle(270,220,70,20));
bhapus.setBounds(new Rectangle(350,220,70,20));
tkolomcari.setBounds(new Rectangle(10,430,100,20));
tcari.setBounds(new Rectangle(115,430,100,20));
bcari.setBounds(new Rectangle(220,430,70,20));
bprev.setBounds(new Rectangle(315,430,50,20));
bnext.setBounds(new Rectangle(370,430,50,20));
ltabel.setBounds(new Rectangle(10,225,150,20));
scroll.setBounds(new Rectangle(10,275,410,150));
btutup.setBounds(new Rectangle(350,460,70,20));
//mengatur atribut komponen teks input
ljudul.setFont(new Font("Impact",1,18));
ljudul.setForeground(Color.magenta);
//memberi nilai pada setiap komponen pilihan,komponen tanggal dan tkolomcari
ttglh.addItem("");
for (int i=1;i<=31;i++){
String t=Integer.toString(i);
if(i<10){
t="0"+t;
}
ttglh.addItem(t);
}
ttglb.addItem("");
ttglb.addItem("Januari");
ttglb.addItem("Februari");
ttglb.addItem("Maret");
ttglb.addItem("April");
ttglb.addItem("Mei");
ttglb.addItem("Juni");
ttglb.addItem("Juli");
ttglb.addItem("Agustus");
ttglb.addItem("September");
ttglb.addItem("Oktober");
ttglb.addItem("November");
ttglb.addItem("Desember");
ttglt.addItem("");
for (int i=2005;i<(Calendar.getInstance().get(Calendar.YEAR));i++){
ttglt.addItem(Integer.toString(i));
}
tkolomcari.addItem("::Pilih Kolom::");
tkolomcari.addItem("Semua");
tkolomcari.addItem("kode");
tkolomcari.addItem("nama");
tkolomcari.addItem("tgl_beli");
tkolomcari.addItem("spesifikasi");
tkolomcari.addItem("harga");
tkolomcari.addItem("stok");
//making table
Object[]row={"no","kode","nama","tanggal beli","spesifikasi","harga","stok"};
tkolomcari.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie){
pilihKolomCari();
}
});
//adding KeyListener in txt component
tkode.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent ke){
if(ke.getKeyCode()==ke.VK_ENTER){
if(tkode.getText().equals("")){
}else{
tnama.requestFocus();
}
}
}
});
tnama.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent ke){
if(ke.getKeyCode()==ke.VK_ENTER){
if(tnama.getText().equals("")){
}else{
ttglh.requestFocus();
}
}
}
});
ttglh.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent ke){
if(ke.getKeyCode()==ke.VK_ENTER){
if(ttglh.getSelectedItem().equals("")){
}else{
ttglb.requestFocus();
}
}
}
});
ttglb.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent ke){
if(ke.getKeyCode()==ke.VK_ENTER){
if(ttglb.getSelectedItem().equals("")){
}else{
ttglt.requestFocus();
}
}
}
});
ttglt.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent ke){
if(ke.getKeyCode()==ke.VK_ENTER){
if(ttglt.getSelectedItem().equals("")){
}else{
tspek.requestFocus();
}
}
}
});
tharga.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent ke){
if(ke.getKeyCode()==ke.VK_ENTER){
if(tharga.getText().equals("")){
}else{
tstok.requestFocus();
}
}
}
});
tstok.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent ke){
if(ke.getKeyCode()==ke.VK_ENTER){
if(tstok.getText().equals("")){
}else{
bsimpan.requestFocus();
}
}
}
});
//adding action listener to every button component;
btambah.addActionListener(this);
bsimpan.addActionListener(this);
bbatal.addActionListener(this);
bubah.addActionListener(this);
bhapus.addActionListener(this);
bcari.addActionListener(this);
bnext.addActionListener(this);
bprev.addActionListener(this);
btutup.addActionListener(this);
//adding every designed component into container Frame
this.getContentPane().setLayout(null);
getContentPane().add(ljudul);
getContentPane().add(lkode);
getContentPane().add(tkode);
getContentPane().add(txtkode);
getContentPane().add(lnama);
getContentPane().add(tnama);
getContentPane().add(ltgl);
getContentPane().add(ttglh);
getContentPane().add(ttglb);
getContentPane().add(ttglt);
getContentPane().add(lspek);
getContentPane().add(tspek);
getContentPane().add(lharga);
getContentPane().add(tharga);
getContentPane().add(lstok);
getContentPane().add(tstok);
getContentPane().add(btambah);
getContentPane().add(bsimpan);
getContentPane().add(bbatal);
getContentPane().add(bubah);
getContentPane().add(bhapus);
getContentPane().add(tkolomcari);
getContentPane().add(tcari);
getContentPane().add(bnext);
getContentPane().add(bprev);
getContentPane().add(ltabel);
getContentPane().add(scroll);
getContentPane().add(btutup);
//giving initialitation value to aplication
txtkode.setEnabled(false);
tblbarang.setEnabled(false);
tcari.setEnabled(false);
bcari.setEnabled(false);
bprev.setEnabled(false);
bnext.setEnabled(false);
teksFalse();
tombolFalse();
koneksiDb koneksi=new koneksiDb();
koneksi.panggilDriver();
tampilTabel();
}
public void actionPerformed(ActionEvent tombol){
if(tombol.getSource()==btambah){
teksTrue();
simpan=true;
tkode.setVisible(true);
txtkode.setVisible(true);
bsimpan.setEnabled(true);
bbatal.setEnabled(true);
btambah.setEnabled(true);
tkolomcari.setEnabled(true);
btutup.setEnabled(true);
tkode.requestFocus();
}
if(tombol.getSource()==bsimpan){
if(simpan==true){
if(tkode.getText().equals("")||tnama.getText().equals("")||ttglt.getSelectedItem().equals("")){
JOptionPane.showMessageDialog(null, "Isi semua data"+"nkode, nama dan tanggal"+"nwajib diisi");
}else{
prosesSimpan();
tombolFalse();
teksFalse();
teksKosong();
btambah.setEnabled(true);
tkolomcari.setEnabled(true);
btutup.setEnabled(true);
}
}else{
tombolUbah();
tampilTabel();
teksKosong();
tombolFalse();
teksFalse();
btambah.setEnabled(true);
tkolomcari.setEnabled(true);
btutup.setEnabled(true);
}
}
if(tombol.getSource()==bbatal){
tombolFalse();
teksKosong();
teksFalse();
btambah.setEnabled(true);
tkolomcari.setEnabled(true);
btutup.setEnabled(true);
}
if(tombol.getSource()==bcari){
tombolCari();
txtkode.setVisible(true);
tkode.setVisible(true);
tkolomcari.setSelectedItem("::Pilih Kolom::");
tcari.setText("");
}
if(tombol.getSource()==bnext){
tombolNext();
}
if(tombol.getSource()==bprev){
tombolPrev();
}
if(tombol.getSource()==bubah){
teksTrue();
simpan=false;
bubah.setEnabled(false);
bhapus.setEnabled(false);
btambah.setEnabled(false);
tkolomcari.setEnabled(false);
bprev.setEnabled(false);
bnext.setEnabled(false);
bsimpan.setEnabled(true);
}
if(tombol.getSource()==bhapus){
prosesHapus();
bprev.setEnabled(false);
bnext.setEnabled(false);
btambah.setEnabled(true);
btutup.setEnabled(true);
bubah.setEnabled(false);
bhapus.setEnabled(false);
bbatal.setEnabled(false);
}
if(tombol.getSource()==btutup){
System.exit(0);
}
}
void tombolFalse(){
bsimpan.setEnabled(false);
bbatal.setEnabled(false);
bubah.setEnabled(false);
bhapus.setEnabled(false);
bnext.setEnabled(false);
bprev.setEnabled(false);
}
void teksFalse(){
tkode.setEnabled(false);
tnama.setEnabled(false);
ttglh.setEnabled(false);
ttglb.setEnabled(false);
ttglt.setEnabled(false);
tharga.setEnabled(false);
tspek.setEnabled(false);
tstok.setEnabled(false);
}
void teksKosong(){
tkode.setText("");
txtkode.setText("");
tnama.setText("");
ttglh.setSelectedItem("");
ttglb.setSelectedItem("");
ttglt.setSelectedItem("");
tharga.setText("");
tspek.setText("");
tstok.setText("");
}
void teksTrue(){
tkode.setEnabled(true);
tnama.setEnabled(true);
ttglh.setEnabled(true);
ttglb.setEnabled(true);
ttglt.setEnabled(true);
tharga.setEnabled(true);
tspek.setEnabled(true);
tstok.setEnabled(true);
}
void prosesSimpan(){
try{
Connection connect=new koneksiDb().condb();
String sql="INSERT INTO tblbarang values(?,?,?,?,?,?)";
PreparedStatement ps=connect.prepareStatement(sql);
try{
ps.setString(1, tkode.getText());
simpanData(ps,0);
}
catch(SQLException sq){
}
tampilTabel();
ps.close();
}
catch(Exception e){
System.out.println("Pesan error:"+e);
}
}
void simpanData(PreparedStatement ps, int u){
try{
ps.setString(2-u, tnama.getText());
String hr=String.valueOf(ttglh.getSelectedItem());
String bln=String.valueOf(ttglb.getSelectedItem());
String thn=String.valueOf(ttglt.getSelectedItem());
String tgl=hr+""+bln+""+thn;
ps.setString(3-u, tgl);
ps.setString(4-u, tspek.getText());
ps.setString(5-u, tstok.getText());
ps.executeUpdate();
}
catch(SQLException e){
System.out.println("Gagal menyimpan SQL error:"+e);
JOptionPane.showMessageDialog(null, "Gagal menyimpan kode kembar:"+"kode["+tkode.getText()+"] telah ada");
tkode.setFocusable(true);
}
}
void pilihKolomCari(){
if(tkolomcari.getSelectedItem().equals("::Pilih Kolom::")){
bcari.setEnabled(false);
tcari.setEnabled(false);
}else{
//jika memilih semua
if(tkolomcari.getSelectedItem().equals("Semua")){
bcari.setEnabled(true);
tcari.setEnabled(false);
}else{
bcari.setEnabled(true);
tcari.setEnabled(true);
tcari.requestFocus();
}
}
}
void tombolCari(){
int r=tabMode.getRowCount();
if(r==0){
JOptionPane.showMessageDialog(null, "Database Kosong","Pesan",1);
}else{
if(tcari.isEnabled()==true){
if(tcari.getText().equals("")){
JOptionPane.showMessageDialog(null, "Masukkan Parameter Pencarian","Pesan",1);
}else{
String colom=tkolomcari.getSelectedItem().toString();
String param=tcari.getText();
String sql="SELECT * FROM tblbarang WHERE "+colom+"="+param+"";
prosesCari(sql);
int e=tabMode.getRowCount();
if(e<=0){
JOptionPane.showMessageDialog(null, "Data"+param+"Tidak ditemukan","Pesan",1);
tampilTabel();
}else{
aturTombolCari();
tampilTeks(0);
}
}
}else{
tampilTabel();
tampilTeks(0);
aturTombolCari();
}
}
}
void aturTombolCari(){
bubah.setEnabled(true);
bhapus.setEnabled(true);
bbatal.setEnabled(true);
bcari.setEnabled(true);
btambah.setEnabled(true);
bprev.setEnabled(true);
bnext.setEnabled(true);
btutup.setEnabled(true);
}
void prosesCari(String sql){
tabelKosong();
int a=1;
try{
Connection connect=new koneksiDb().condb();
Statement stat=connect.createStatement();
ResultSet rset=stat.executeQuery(sql);
while(rset.next()){
String kode=rset.getString(1);
String nama=rset.getString(2);
String tgl=rset.getString(3);
String spek=rset.getString(4);
String harga=rset.getString(5);
String stok=rset.getString(6);
String no=Integer.toString(a);
String[] data={no,kode,nama,tgl,spek,harga,stok};
tabMode.addColumn(data);
a++;
}
stat.close();
}catch(SQLException e){
}
}
public void tabelKosong(){
int row=tabMode.getRowCount();
for(int i=0;i<row;i++){
tabMode.removeRow(0);
}
}
void tampilTabel(){
String sql="SELECT * FROM tblbarang";
prosesCari(sql);
}
void tampilTeks(int row){
String kode=tabMode.getValueAt(row, 1).toString();
String nama=tabMode.getValueAt(row, 2).toString();
String tgl=tabMode.getValueAt(row, 3).toString();
int i=tgl.length();
int x=i-4;
Object tanggal=tgl.substring(0,2);
Object bulan=tgl.substring(3,x-1);
Object tahun=tgl.substring(x,i);
String spek=tabMode.getValueAt(row, 4).toString();
String harga=tabMode.getValueAt(row, 5).toString();
String stok=tabMode.getValueAt(row, 6).toString();
txtkode.setText(kode);
tnama.setText(nama);
ttglh.setSelectedItem(tanggal);
ttglb.setSelectedItem(bulan);
ttglt.setSelectedItem(tahun);
tspek.setText(spek);
tharga.setText(harga);
tstok.setText(stok);
}
void tombolNext(){
++n;
int c=tabMode.getRowCount();
if(n>c-1){
n=c-1;
JOptionPane.showMessageDialog(null, "Record Terakhir");
}
tampilTeks(n);
}
void tombolPrev(){
--n;
if(n<0){
n=0;
JOptionPane.showMessageDialog(null, "Record pertama");
}
tampilTeks(n);
}
void tombolUbah(){
String kode=txtkode.getText();
String nama=tnama.getText();
String message="Anda yakin ingin mengubah data"+"kode:"+kode+"nama"+nama;
int pesan=JOptionPane.showConfirmDialog(null, message,"Ubah Data",JOptionPane.YES_NO_OPTION);
if(pesan==0){
try{
Connection connect=new koneksiDb().condb();
String param=tkode.getText();
String sql="UPDATE tblbarang SET nama=?,tgl_beli=?,spek=?,harga=?,stok=? WHERE kode="+param+"";
PreparedStatement ps=connect.prepareStatement(sql);
simpanData(ps,1);
}catch(SQLException e){
System.out.println(e);
}
}
}
void prosesHapus(){
String kode=txtkode.getText();
String nama=tnama.getText();
String spek=tspek.getText();
String harga=tharga.getText();
try{
String message="Apakah Anda Yakin Menghapus Data"+"kode: "+kode+ "nama: "+nama+"spek: "+spek+"harga: "+harga;
int pesan=JOptionPane.showConfirmDialog(null, message,"Konfirmasi Hapus Data",JOptionPane.YES_NO_OPTION);
if(pesan==0){
Connection connect=new koneksiDb().condb();
String sql="DELETE FROM tblbarang WHERE kode="+kode+"";
PreparedStatement pstat=connect.prepareStatement(sql);
pstat.executeUpdate();
tampilTabel();
teksKosong();
}
}catch(SQLException se){
String message="Gagal Menghapus Data"+"kode : "+kode+"nama : "+nama+"Error : "+se;
JOptionPane.showMessageDialog(null, message);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
aplikasiBarang tampilan=new aplikasiBarang();
tampilan.setTitle("Program Aplikasi Adventori Barang");
tampilan.setSize(440,530);
Dimension layar=Toolkit.getDefaultToolkit().getScreenSize();
int l=(layar.width-tampilan.getSize().width)/2;
int t=(layar.height-tampilan.getSize().height)/2;
tampilan.setLocation(l, t);
tampilan.setResizable(false);
tampilan.setVisible(true);
tampilan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}