1.Langkah pertama tentunya kita buat terlebih dahulu tampilan
formnya seperti pada gambar.
2.Kemudian barulah kita masukan teks kode berikut pada tombol
save.
btnSave.addActionListener(new ActionListener() {
public
void actionPerformed(ActionEvent arg0) {
if
(arg0.getSource() == btnSave){
do_btnSave_actionPerformed(arg0);
}
}
private
void do_btnSave_actionPerformed(ActionEvent arg0) {
if
(!FieldNoResi.getText().equals("") &&
!FieldKetPaket.getText().equals("") &&
!FieldTujuan.getText().equals("") &&
!FieldNoTelp.getText().equals(""))
{
vnNO = vnNO + 1;
ambilData(vnNO, tabelModel);
}
else {
JOptionPane.showMessageDialog(null,
"Data harus diisi semua!");
}
}
private
void ambilData(int vnNO, DefaultTableModel tabelModel)
try{
table.setModel(tabelModel);
String
kolom1 = String.valueOf(vnNO).toString();
String
kolom2 = FieldNoResi.getText();
String
kolom3 = FieldKetPaket.getText();
String
kolom4 = FieldTujuan.getText();
String
kolom5 = FieldNoTelp.getText();
String[]
data = {kolom1, kolom2, kolom3, kolom4, kolom5};
tabelModel.addRow(data);
FieldNoResi.setText("");
FieldKetPaket.setText("");
FieldTujuan.setText("");
FieldNoTelp.setText("");
}
catch (Exception ex) {
JOptionPane.showMessageDialog(table, this, "Data gagal disimpan! :
" +ex, vnNO);
}
}
});
3.Dan berikut hasil jadinya.
3.Dan ini teks kode untuk tombol delete nya.
btnDelete.addActionListener(new ActionListener() {
private
int row;
public
void actionPerformed(ActionEvent arg0) {
if
(arg0.getSource() == btnDelete){
do_btnDelete_actionPerformed(arg0);
}
}
private
void do_btnDelete_actionPerformed(ActionEvent arg0) {
try {
int barisVal;
String kolom1Val;
int jmlBaris = table.getRowCount();
table.setRowSelectionAllowed(true);
row = table.getSelectedRow();
tabelModel.removeRow(row);
vnNO = vnNO - 1;
for (int baris = 0; baris <
jmlBaris; baris++) {
barisVal = baris + 1;
kolom1Val =
String.valueOf(barisVal).toString();
tabelModel.setValueAt(kolom1Val,
baris, 0);
}
}
catch (Exception ex) {}
}
});
0 Response to "Cara Menampilkan Data Sementara Ke Tabel Tanpa Menggunakan Database Pada Java Eclipse"
Post a Comment