Sunday, 29 November 2009

Tutorial Jtable

Berikut Ini Saya Share Sedikit Ilmu Yang Saya Pelajari Dari Om Google, Cukup Sulit Awalnya Tapi Kalau Sudah Terbiasa Pasti Jadi Muda. Dimana Tutorial Jtable Tersebut Meliputi Penambahan Baris, Hapus Baris Dan Isi Data Pada Baris Perbaris. Untuk Lebih Jelasnya Lihat Gambar Di Bawah Ini :



Berikut Source Code Lengkapnya :

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
*
* @author Mr.Bayu
*/
public class utama extends javax.swing.JFrame {
DefaultTableModel model;
Object data[][]=null;
int i;
public utama() {
initComponents();
model = (DefaultTableModel) tabel.getModel();
}
@SuppressWarnings("unchecked")
//
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
tno_hp = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
tnama = new javax.swing.JTextField();
talamat = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
tabel = new javax.swing.JTable();
btambah = new javax.swing.JButton();
bhapus = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Demo Jtable");
setLocationByPlatform(true);
setResizable(false);
getContentPane().setLayout(null);

jPanel1.setBackground(new java.awt.Color(0, 255, 0));
jPanel1.setLayout(null);
jPanel1.add(tno_hp);
tno_hp.setBounds(110, 80, 230, 20);

jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel1.setText("No HP");
jPanel1.add(jLabel1);
jLabel1.setBounds(10, 80, 90, 17);

jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel2.setText("Nama");
jPanel1.add(jLabel2);
jLabel2.setBounds(10, 20, 90, 17);

jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel3.setText("Alamat");
jPanel1.add(jLabel3);
jLabel3.setBounds(10, 50, 90, 17);
jPanel1.add(tnama);
tnama.setBounds(110, 20, 230, 20);
jPanel1.add(talamat);
talamat.setBounds(110, 50, 230, 20);

tabel.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null}
},
new String [] {
"Nama", "Alamat", "No HP"
}
));
tabel.setRowSelectionInterval(tabel.getRowCount()-1, tabel.getRowCount()-1);
jScrollPane1.setViewportView(tabel);

jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(10, 140, 430, 190);

btambah.setText("Tambah");
btambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btambahActionPerformed(evt);
}
});
jPanel1.add(btambah);
btambah.setBounds(10, 110, 90, 23);

bhapus.setText("Hapus");
bhapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bhapusActionPerformed(evt);
}
});
jPanel1.add(bhapus);
bhapus.setBounds(110, 110, 73, 23);

getContentPane().add(jPanel1);
jPanel1.setBounds(0, 0, 450, 340);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-456)/2, (screenSize.height-372)/2, 456, 372);
}//


private void btambahActionPerformed(java.awt.event.ActionEvent evt) {
int nama = tnama.getText().length();
int alamat = talamat.getText().length();
int nohp = tno_hp.getText().length();
if(nama==0|alamat==0|nohp==0)
{
JOptionPane.showMessageDialog(rootPane, "Terdapat Kolom Yang Kosong!", "Informasi", JOptionPane.INFORMATION_MESSAGE);
}
else
{
tabel.setValueAt(tnama.getText(), tabel.getSelectedRow(), 0);
tabel.setValueAt(talamat.getText(), tabel.getSelectedRow(), 1);
tabel.setValueAt(tno_hp.getText(), tabel.getSelectedRow(), 2);
model.addRow(data);
tabel.setRowSelectionInterval(tabel.getRowCount()-1, tabel.getRowCount()-1);
}
}

private void bhapusActionPerformed(java.awt.event.ActionEvent evt) {
model.removeRow(tabel.getSelectedRow());
if(tabel.getRowCount()==0)
{
model.addRow(data);
}
tabel.setRowSelectionInterval(tabel.getRowCount()-1, tabel.getRowCount()-1);
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new utama().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton bhapus;
private javax.swing.JButton btambah;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tabel;
private javax.swing.JTextField talamat;
private javax.swing.JTextField tnama;
private javax.swing.JTextField tno_hp;
// End of variables declaration

}

No comments:

Post a comment