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:
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
tambahan tugas web
Delicious Strawberry
Info Beasiswa Luar Negri
Blue Choclote
Pink Rolls Choclate
Designed by dewi rohmani | Sponsored by : unindra 2014
0 komentar :
Posting Komentar