Tempat Berbagi Ilmu dan Pengalaman

Blog ini menampilkan berbagai artikel artikel yang dapat menambah pengetahuan dan membantu para mahasiswa untuk meningkatkan pengetahuan dan membantu tugas kuliah

Pemrograman JTabel dengan ActionListener

JTable extend Action Listener


import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;

public class CobaKodeTabel extends JFrame implements ActionListener
{
    JLabel lbljudul = new JLabel("DATA BARANG");
    JLabel lbkode = new JLabel("Kode Barang ");
    JLabel lbnamab = new JLabel("Nama Barang ");
    JLabel lbjenis = new JLabel("Jenis ");
    JLabel lbharga = new JLabel("Harga");
    JLabel lbjumlah = new JLabel("Jumlah");
   
   
    JTextField tfkode = new JTextField();
    JTextField tfnamab = new JTextField();
    JTextField tfjenis = new JTextField();
    JTextField tfharga = new JTextField();
    JTextField tfjumlah = new JTextField();
   
    JButton btsimpan = new JButton("SIMPAN");
    JButton bthapus = new JButton("HAPUS");
    JButton btexit = new JButton("KELUAR");
    JButton btedit = new JButton("EDIT");
    JButton btupdate = new JButton("UPDATE");
   
       
    DefaultTableModel model = new DefaultTableModel();
    JTable tampil = new JTable(model);
    JScrollPane pane = new JScrollPane(tampil);
      
    public CobaKodeTabel()
   
    {  
        super("CobaKodeTabel");
       
lbljudul.setFont(new Font("Arial",Font.BOLD,20));
lbljudul.setBounds(150,5,210,20);          tfkode.setBounds(120,30,100,25);   
lbkode.setBounds(30,30,100,25);          btsimpan.setBounds(250,30,100,25);
lbnamab.setBounds(30,60,100,25);        tfnamab.setBounds(120,60,100,25);                           bthapus.setBounds(250,60,100,25);       tfjenis.setBounds(120,90,100,25);    
lbjenis.setBounds(30,90,100,25);           btexit.setBounds(250,90,100,25);
lbharga.setBounds(30,120,100,25);       tfharga.setBounds(120,120,100,25);           btedit.setBounds(250,120,100,25);        tfjumlah.setBounds(120,150,100,25);    lbjumlah.setBounds(30,150,100,25);      btupdate.setBounds(250,150,100,25);                                         pane.setBounds(10,200,480,250);
       
        add(lbkode);         add(tfkode);          add(btsimpan);     add(lbljudul); 
        add(lbnamab);       add(tfnamab);       add(bthapus);       add(btupdate);  
        add(lbjenis);          add(tfjenis);          add(btexit);           add(tfjumlah);      
        add(lbharga);        add(tfharga);         add(btedit);          add(lbjumlah); 
     
        add(pane);  
       
        btsimpan.addActionListener(this);
        bthapus.addActionListener(this);
        btexit.addActionListener(this);
        btedit.addActionListener(this);
        btupdate.addActionListener(this);
       
        setLayout(null);
        setSize(550,500);
        setVisible(true);
       
        String header [] = {"Kode", "Nama Barang", "Jenis", "Harga", "Jumlah"};
        for(int kolom =0; kolom

        {
            model.addColumn(header[kolom]);
        }
    }
   
    public void actionPerformed(ActionEvent ae)
    {
        String dataKode_Barang = tfkode.getText();
        String dataNama_Barang= tfnamab.getText();
        String dataJenis = tfjenis.getText();
        String dataHarga = tfharga.getText();
        String dataJumlah = tfjumlah.getText();
      

        if(ae.getSource() == btsimpan)
        {
            PreparedStatement ps = null;
            try
            {
                      
            Class.forName("org.sqlite.JDBC");
            Connection koneksi = DriverManager.getConnection("JDBC:sqlite:kuis.db");
            ps = koneksi.prepareStatement("insert into databarang(kode,nama_barang,jenis,harga,Jumlah)          values (?,?,?,?,?)");
            ps.setString(1,dataKode_Barang);
            ps.setString(2,dataNama_Barang);
            ps.setString(3,dataJenis);
            ps.setString(4,dataHarga);
            ps.setString(5,dataJumlah);
            ps.executeUpdate();
            JOptionPane.showMessageDialog(null,"Data Telah Tersimpan");
           
            int row=model.getRowCount ();
                for (int i = 0; i < row; i++)
                {
                    model.removeRow(0);
                }
               
            Statement stat = koneksi.createStatement ();
            ResultSet set = stat.executeQuery("select * from databarang");
           
                while(set.next())
                { model.addRow(new Object [] {set.getString("kode"),set.getString("nama_barang"),set.getString("jenis"),
                    set.getString("harga"),set.getString("jumlah")});
                    tampil.setModel(model);
                }
                tfkode.setText("");
                tfnamab.setText("");
                tfjenis.setText("");
                tfharga.setText("");
                tfjumlah.setText("");
            }catch(Exception ex)
            {
                System.err.println("Error...!SIMPAN!..");
            }
              
        }
       
        else if(ae.getSource() == bthapus)
        {
            tfkode.setText("");
            tfnamab.setText("");
            tfjenis.setText("");
            tfharga.setText("");
            tfjumlah.setText("");
            tfkode.requestFocus();
        }
       
        else if (ae.getSource() == btedit)
        {
            int baris = tampil.getSelectedRow();
           
            String kolomKode_Barang = model.getValueAt(baris, 0).toString();
            tfkode.setText(kolomKode_Barang);
           
            String kolomNama_Barang = model.getValueAt(baris, 1).toString();
            tfnamab.setText(kolomNama_Barang);
           
            String kolomJenis = model.getValueAt(baris, 2).toString();
            tfjenis.setText(kolomJenis);
           
            String kolomHarga = model.getValueAt(baris, 3).toString();
            tfharga.setText(kolomHarga);  
            String kolomJumlah = model.getValueAt(baris, 3).toString();
            tfjumlah.setText(kolomJumlah);  
        }
        else if (ae.getSource() == btupdate)
        {
            //model.insertRow(0, new Object[] {dataNAMA,dataNPM,dataALAMAT,dataKET});
          
        }
              
        else if(ae.getSource() == btexit)
        {
            System.exit(0);
            //new CobaAksiLoginNoDB();
        }
       
       
    }
   
    public static void main (String [] OkeLah)
    {
        CobaKodeTabel cat = new CobaKodeTabel();
    }
}
output:


Pemrograman CRUD

 Pemrograman Visual CRUD dengan Koneksi JDBC:sqlite3

import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Font;
import java.awt.Color;
import java.sql.*;

    public class sewalosmen1 extends JFrame implements ActionListener
    {
            JLabel lbljudul = new JLabel("PEMESANAN KAMAR");
            JLabel lblIDPelanggan = new JLabel("ID Pelanggan       :");
            JLabel lblNama = new JLabel("Nama                    :");
            JLabel lblTgl = new JLabel("Tanggal Cek In    :");
            JLabel lblAlamat = new JLabel("Alamat                  :");
            JLabel lblJenisKamar = new JLabel("JenisKamar        :");
            JLabel lblMember = new JLabel("Member                :");
            JLabel lbllama = new JLabel("Lama Menginap :");
            JLabel lblTelp = new JLabel("Telp                       :");

            JTextField tfIDPelanggan = new JTextField();
            JTextField tfNama = new JTextField();
            JTextField tfTgl = new JTextField();
            JTextField tfAlamat = new JTextField();

            ButtonGroup btnGrup=new ButtonGroup();
            JRadioButton rDeluxeRoom = new JRadioButton("1. Deluxe Room Rp 275.000/hari");
            JRadioButton rSuiteRoom = new JRadioButton("2. Suite Room Rp 300.000/hari");
            JRadioButton rPresidentRoom = new JRadioButton("3. President Room Rp 370.000/hari");
                  
            ButtonGroup btnGrup1=new ButtonGroup();
            JRadioButton rMember = new JRadioButton("Member");
            JRadioButton rNonMember = new JRadioButton("Non Member");
       
            JTextField tflama = new JTextField();
            JTextField tfTelp = new JTextField();

            JButton btsimpan = new JButton("SIMPAN");
            JButton bthapus = new JButton("HAPUS");
            JButton btexit = new JButton("KELUAR");
            JButton btedit = new JButton("EDIT");
            JButton btupdate = new JButton("UPDATE");
     
            DefaultTableModel model = new DefaultTableModel();
            JTable tampil = new JTable(model);
            JScrollPane pane = new JScrollPane(tampil);
          
            DefaultTableModel model1 = new DefaultTableModel();
            JTable tampil1 = new JTable(model1);
            JScrollPane pane1 = new JScrollPane(tampil1);
               
  public sewalosmen1()
            {
                super("sewalosmen1");
              
                setVisible(true);
                setSize(850,800);
                setLocationRelativeTo(null);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setLayout(null);
              
                lbljudul.setFont(new Font("Monotype Corsiva",Font.BOLD,25));
 lbljudul.setForeground(Color.blue);                       lbljudul.setBounds(250,10,300,25);
 lblIDPelanggan.setBounds(20,50,100,25);            tfIDPelanggan.setBounds(150,50,200,25);    btsimpan.setBounds(500,50,100,25);                    lblNama.setBounds(20,80,100,25);                   tfNama.setBounds(150,80,200,25);                      bthapus.setBounds(500,80,100,25                    lbllama.setBounds(20,110,120,25);                       tflama.setBounds(150,110,200,25);                   btupdate.setBounds(500,110,100,25);                  lblJenisKamar.setBounds(20,140,100,25);         tfTgl.setBounds(150,270,200,25);                        btedit.setBounds(500,140,100,25);                    lblMember.setBounds(20,210,100,25);                 tfAlamat.setBounds(150,300,200,25);                btexit.setBounds(500,170,100,25);                       lblTgl.setBounds(20,270,100,25);                      tfTelp.setBounds(150,330,200,25);                       lblTelp.setBounds(20,330,100,25);     
lblAlamat.setBounds(20,300,100,25);                   btsimpan.setBackground(new Color(102,255,102));  
btupdate.setBackground(new Color(102,255,102));
btsimpan.setForeground(Color.red);                        btupdate.setForeground(Color.red); pane.setBounds(20,380,650,500);                          btsimpan.setFont(new java.awt.Font("Chili",1,12));       btupdate.setFont(new java.awt.Font("Chili",1,12));  pane1.setBounds(670,380,170,500);           bthapus.setBackground(new Color(102,255,102));  btexit.setBackground(new Color(102,255,102));
bthapus.setForeground(Color.red);                           btexit.setForeground(Color.red);
bthapus.setFont(new java.awt.Font("Chili",1,12));    btexit.setFont(new java.awt.Font("Chili",1,12));
rDeluxeRoom.setBounds(150,140,220,25);             btedit.setBackground(new Color(102,255,102));
rSuiteRoom.setBounds(150,160,220,25);                btedit.setForeground(Color.red);
rPresidentRoom.setBounds(150,180,220,25);         btedit.setFont(new java.awt.Font("Chili",1,12));
rMember.setBounds(150,210,190,25);                   pane.setBackground(new Color(98,115,70));
rNonMember.setBounds(150,230,190,25);            pane1.setBackground(new Color(98,115,70));
                                                         
 add(lbljudul);                        add(pane);                   add(rDeluxeRoom);          add(pane1);  add(lblIDPelanggan);             add(tfIDPelanggan);     add(rSuiteRoom);             add(rNonMember);  add(lblNama);                       add(tfNama);               add(rPresidentRoom);       add(btexit);   
add(lbllama);                         add(tflama);                 add(rMember);                  add(btedit);
add(lblJenisKamar);              add(tfTgl);                   add(btsimpan);                   add(lblTelp);   
add(lblMember);                  add(tfAlamat);             add(bthapus);                     add(lblAlamat);      
add(lblTgl);                          add(tfTelp);                  add(btupdate);
                                                                                                              
                btsimpan.addActionListener(this);
                bthapus.addActionListener(this);
                btexit.addActionListener(this);
                btedit.addActionListener(this);
                btupdate.addActionListener(this);
    
                btnGrup.add(rDeluxeRoom);
                btnGrup.add(rSuiteRoom);
                btnGrup.add(rPresidentRoom);
                rDeluxeRoom.setSelected(true);

                btnGrup1.add(rMember);
                btnGrup1.add(rNonMember);
                rMember.setSelected(true);                 
     
                String header [] = {"ID_Pel", "Nama","JenKam","Lama", "Member","TglCekIn","Alamat","Telp"};
                for(int kolom =0; kolom



                {
                     model.addColumn(header[kolom]);
                }
                 String header1 [] = {"Tarif","Diskon","Bayar"};
                for(int kolom =0; kolom                {
                     model1.addColumn(header1[kolom]);
                }
              
            }
                @Override
            public void actionPerformed(ActionEvent act)
            {                          
            
            double lama=Double.parseDouble(tflama.getText());
            double tarif=0,diskon=0,bayar=0;
            Object obj = act.getSource();
          
        String dataID_Pel = tfIDPelanggan.getText();
        String dataNama= tfNama.getText();
        String dataLama= tflama.getText();
        String dataTglCekin =tfTgl.getText();
        String dataAlamat=tfAlamat.getText();
        String dataTelp=tfTelp.getText();
        String []dataJenKam={"rDeluxeRoom","rSuiteRoom","rPresidentRoom"};
        String []dataMember={"rMember","rNonMember"};
       
            if (obj==btsimpan)
            {
                String JenKam="";
                String Member="";
                {

           if (rDeluxeRoom.isSelected())
                    {
                        tarif = lama*275000;
                        JenKam="Deluxe Room";
                    } 
                    else if (rSuiteRoom.isSelected())
                    {
                        tarif = lama*300000;
                        JenKam="Suite Room";
                    }
                        else
                        {
                        tarif = lama*375000;
                        JenKam="President Room";
                    }

           if (rMember.isSelected())
           {
                    Member = " Member";
                    if(lama>=4 && lama<=7)
                    {
                      diskon = 0.20 * tarif;
                    }
                    else if (lama>7)
                    {
                        diskon = 0.35 * tarif;
                    }
                }
           else if
                (rNonMember.isSelected())
                    {
                        Member = "Non Member";
                            if (lama>=4 && lama<=7)
                            {
                                diskon = 0.10 * tarif;
                            }
                            else if (lama>7)
                            {
                               diskon = 0.15 * tarif;
                            }
           }

               bayar = tarif-diskon;
                String [][] data = {{dataID_Pel,dataNama,JenKam,dataLama,Member,dataTglCekin,dataAlamat,dataTelp}};
                Double [][] isSelected ={{tarif,diskon,bayar}};
               for(int isi=0; isi            {
                model.addRow(data[isi]);
            }
        
            for(int isi=0; isi            {
                model1.addRow(isSelected[isi]);
            }
              
             PreparedStatement ps = null;
             PreparedStatement ps1 = null;
            try
            {
                 Class.forName("org.sqlite.JDBC");
            Connection koneksi = DriverManager.getConnection("JDBC:sqlite:produk.db");
            ps = koneksi.prepareStatement("insert into sewalos(ID_Pel,Nama,JenKam,Lama,Member,TglCekIn,Alamat,Telp) values (?,?,?,?,?,?,?,?)");
            ps.setString(1,dataID_Pel);
            ps.setString(2,dataNama);
            ps.setString(3,JenKam);
            ps.setString(4,dataLama);
            ps.setString(5,Member);
            ps.setString(6,dataTglCekin);
            ps.setString(7,dataAlamat);
            ps.setString(8,dataTelp);
         
            ps.executeUpdate();
          
            ps1 = koneksi.prepareStatement("insert into sewalosmen(Tarif,Diskon,Bayar) values (?,?,?)");
            ps1.setDouble(1,tarif);
            ps1.setDouble(2,diskon);
            ps1.setDouble(3,bayar);
                     
            ps1.executeUpdate();
          
            JOptionPane.showMessageDialog(null,"Data Telah Tersimpan");
          
            int row=model.getRowCount ();
                for (int i = 0; i < row; i++)
                {
                    model.removeRow(0);
                }
            int row1=model1.getRowCount ();
                for (int i = 0; i < row; i++)
                {
                    model1.removeRow(0);
                }
              
            Statement stat = koneksi.createStatement ();
            ResultSet set = stat.executeQuery("select * from sewalos");
                     
          
                while(set.next())
                { model.addRow(new Object [] {set.getString("ID_Pel"),set.getString("Nama"),set.getString("JenKam"),set.getString("Lama"),
                    set.getString("Member"),set.getString("TglCekIn"),set.getString("Alamat"),set.getString("Telp")});
                    tampil.setModel(model);
                }
               tfIDPelanggan.setText("");
               tfNama.setText("");
               tflama.setText("");
               tfTgl.setText("");
               tfAlamat.setText("");
               tfTelp.setText("");
             
               ResultSet set1 = stat.executeQuery("select * from sewalosmen");
          
                while(set1.next())
                { model1.addRow(new Object [] {set1.getDouble("Tarif"),set1.getDouble("Diskon"),set1.getDouble("Bayar")});
                    tampil1.setModel(model1);
                }
             
             
            }catch(Exception ex)
            {
                System.err.println("Error...!SIMPAN!..");
            }
        }
              
            if(obj == bthapus)
        {
            tfIDPelanggan.setText("");
            tfNama.setText("");
            tflama.setText("");
            tfTgl.setText("");
            tfAlamat.setText("");
            tfTelp.setText("");
            tfIDPelanggan.requestFocus();
        }
      
        else if (obj == btedit)
        {
            int baris = tampil.getSelectedRow();
          
            String kolomID_Pelanggan = model.getValueAt(baris, 0).toString();
            tfIDPelanggan.setText(kolomID_Pelanggan);
          
            String kolomNama = model.getValueAt(baris, 1).toString();
            tfNama.setText(kolomNama);
          
            String kolomLama_Menginap = model.getValueAt(baris, 2).toString();
            tflama.setText(kolomLama_Menginap);
          
            String kolomAlamat = model.getValueAt(baris, 3).toString();
            tfAlamat.setText(kolomAlamat); 
            String kolomTelp = model.getValueAt(baris, 4).toString();
            tfTelp.setText(kolomTelp); 
        }
        else if(obj == bthapus)
        {
            tfIDPelanggan.setText("");
            tfNama.setText("");
            tflama.setText("");
            tfTgl.setText("");
            tfAlamat.setText("");
            tfTelp.setText("");
            tfIDPelanggan.requestFocus();
        }
       
        else if (obj == btupdate)
        {
            //model.insertRow(0, new Object[] {dataNAMA,dataNPM,dataALAMAT,dataKET});
         
        }
       
        else if (obj == btexit)
        {
            System.exit(0);
        }
      
     
     }
  }

  public static void main (String [] form)
    {
            sewalosmen1 sewalosmen1 = new sewalosmen1();
    }
}

output:






Action Listener

Program JTable dengan Action Listener

import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.*;

public class CobaAksiTabel extends JFrame implements ActionListener
{
    JLabel lbnama = new JLabel("Nama Anda ");
    JLabel lbnpm = new JLabel("NPM Anda ");
    JLabel lbalamat = new JLabel("Alamat Anda ");
    JLabel lbket = new JLabel("Keterangan");
    JTextField tfnama = new JTextField();
    JTextField tfnpm = new JTextField();
    JTextField tfalamat = new JTextField();
    JTextField tfket = new JTextField();
   
    JButton btsimpan = new JButton("SIMPAN");
    JButton bthapus = new JButton("HAPUS");
    JButton btexit = new JButton("KELUAR");
    JButton btedit = new JButton("EDIT");
    JButton btupdate = new JButton("UPDATE");
    DefaultTableModel model = new DefaultTableModel();
    JTable tampil = new JTable(model);
    JScrollPane pane = new JScrollPane(tampil);
      
    public CobaAksiTabel()
    {  
        super("CobaAksiTabel");
       
lbnama.setBounds(30,20,100,25);           btexit.setBounds(230,80,100,25);                                                         btsimpan.setBounds(230,20,100,25);
lbnpm.setBounds(30,50,100,25);   
lbalamat.setBounds(30,80,100,25);
lbket.setBounds(30,110,100,25);
btedit.setBounds(230,110,100,25);
tfket.setBounds(120,110,100,25);    
tfnpm.setBounds(120,50,100,25);                                         
bthapus.setBounds(230,50,100,25);                                          
pane.setBounds(350,20,500,117);
tfnama.setBounds(120,20,100,25);     
btupdate.setBounds(120,140,100,25);
tfalamat.setBounds(120,80,100,25); 

        add(lbnama);    add(tfnama);    add(btsimpan);              add(pane); 
        add(lbnpm);     add(tfnpm);      add(bthapus);                add(btupdate);
        add(lbalamat);  add(tfalamat);   add(btexit);
        add(lbket);       add(tfket);        add(btedit);
                   
                       

       
        btsimpan.addActionListener(this);
        bthapus.addActionListener(this);
        btexit.addActionListener(this);
        btedit.addActionListener(this);
        btupdate.addActionListener(this);
       
        setLayout(null);
        setSize(900,220);
        setVisible(true);
       
        String header [] = {"NAMA", "NPM", "ALAMAT", "KETERANGAN"};
        for(int kolom =0; kolom



        {
            model.addColumn(header[kolom]);
        }
    }
   
    public void actionPerformed(ActionEvent ae)
    {
        String dataNAMA = tfnama.getText();
        String dataNPM = tfnpm.getText();
        String dataALAMAT = tfalamat.getText();
        String dataKET = tfket.getText();

        if(ae.getSource() == btsimpan)
        {
            String [][] data = {{dataNAMA,dataNPM,dataALAMAT,dataKET}};
            for(int isi=0; isi            {
                model.addRow(data[isi]);
            }
        }
       
        else if(ae.getSource() == bthapus)
        {
            tfnama.setText("");
            tfnpm.setText("");
            tfalamat.setText("");
            tfket.setText("");
            tfnama.requestFocus();
        }
       
        else if (ae.getSource() == btedit)
        {
            int baris = tampil.getSelectedRow();
           
            String kolomNAMA = model.getValueAt(baris, 0).toString();
            tfnama.setText(kolomNAMA);
           
            String kolomNPM = model.getValueAt(baris, 1).toString();
            tfnpm.setText(kolomNPM);
           
            String kolomALAMAT = model.getValueAt(baris, 2).toString();
            tfalamat.setText(kolomALAMAT);
           
            String kolomKETERANGAN = model.getValueAt(baris, 3).toString();
            tfket.setText(kolomKETERANGAN);           
        }
        else if (ae.getSource() == btupdate)
        {
            //model.insertRow(0, new Object[] {dataNAMA,dataNPM,dataALAMAT,dataKET});
          
        }
       
        else if(ae.getSource() == btexit)
        {
            System.exit(0);
            //new CobaAksiLoginNoDB();
        }
       
       
    }
   
    public static void main (String [] OkeLah)
    {
        CobaAksiTabel cat = new CobaAksiTabel();
    }
}


output:

J Table

CONTOH PROGRAM JTABLE DENGAN BLUE J




import javax.swing.*;
import java.awt.*;

public class ContohJTable extends JFrame
{
    public static void main (String []args)
    {
        ContohJTable apl = new ContohJTable ();
    }
    public ContohJTable ()
    {
        super ("Tes JTable");
        setSize (400,150);
       
        Object data [] [] = {
            {"11234","Astri Damayanti","Akunting", new Long(2200000)},
            {"11235","Eritria","Pemasaran", new Long(1200000)},
            {"11236","Satria","Personalia", new Long(1200000)},
            {"11237","Tri Setyowati","EDP", new Integer(1500000)},
            {"11238","Fahmi Basya","Akunting", new Long(10000000)},
            {"11239","Suadi Marwan","Akunting", new Long(1100000)},
            {"11241"," Listya","Pemasaran", new Long(1100000)},
        };
       
        String [] judulKolom ={"NIP","Nama","Bagian","Gaji"};
        JTable tabel = new JTable(data, judulKolom);
        JScrollPane scrollpane = new JScrollPane(tabel);
        tabel.setPreferredScrollableViewportSize
        (new Dimension(300,50));
       
        getContentPane().add(scrollpane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        //--- Tampilkan frame
        show();
    }
}








OUTPUT: