java - Refresh JTable after delete and insert in JTabbedPane -
i want jtable auto-update when adding or deleting data table, can't refresh: can me find mistakes here?
package gui; public class cek extends jframe implements actionlistener, listselectionlistener { public static connectionmanager conn = new connectionmanager(); private string driver = "com.mysql.jdbc.driver"; private string url = "jdbc:mysql://localhost/db_uas"; private string username = "root"; private string password = "shasa"; defaulttablemodel tablemodel = new defaulttablemodel(); private jtabbedpane tabpane; private jlabel label1; private jpanel panel1,panel2,panel3; private jtable table = new jtable(); private string judulkolom[] = {"mata uang" , "harga"}; private defaulttablemodel tbm; private int nmax = 50; int harga_kamar; listselectionmodel listmod; int row=0; object data[][] = new object[nmax][2]; jlabel lblmatauang = new jlabel("mata uang :"); jtextfield jtfmatauang = new jtextfield(); jlabel lbleditharga = new jlabel("edit harga :"); jtextfield jtfeditharga = new jtextfield(); jbutton jbedit = new jbutton("update"); jlabel lbladdmatauang = new jlabel("mata uang baru :"); jlabel lbladdharga = new jlabel("nilai tukar : rp."); jtextfield jtfaddmatauang = new jtextfield(); jtextfield jtfaddharga = new jtextfield(); jbutton btnadd = new jbutton("add"); jbutton btnpaiipaii = new jbutton("exittt"); jbutton btndelete = new jbutton("delete"); string ket; arraylist<string> shasa1 = new arraylist<>(); jlabel lbldeletemu = new jlabel("delete mata uang : "); public cek(){ super("list data"); setsize(500, 500); setresizable(false); setlocationrelativeto (null); setvisible (true); tabpane = new jtabbedpane (); label1 = new jlabel ("panel pertama", swingconstants.center); jbedit.addactionlistener(this); btnadd.addactionlistener(this); btnpaiipaii.addactionlistener(this); btndelete.addactionlistener(this); for(int i=0;i<data.length;i++){ data[i][0] = new object(); data[i][1] = new object(); } table = new jtable(); tbm = new defaulttablemodel(new object[][]{}, judulkolom); table.setmodel(tbm); table.setbounds(5, 5, 300, 300); tampil(); lblmatauang.setbounds(330, 10, 200, 50); jtfmatauang.setbounds(330, 50, 150, 20); lbleditharga.setbounds(330, 60, 200, 50); jtfeditharga.setbounds(330, 100, 150, 20); jbedit.setbounds(330, 130, 80, 30); btnpaiipaii.setbounds(330, 180, 100, 20); jtfmatauang.seteditable(false); listmod = table.getselectionmodel(); listmod.setselectionmode(listselectionmodel.single_selection); listmod.addlistselectionlistener(this); panel1 = new jpanel (); panel1.setlayout(null); panel1.add(table); panel1.add(lblmatauang); panel1.add(jtfmatauang); panel1.add(lbleditharga); panel1.add(jtfeditharga); panel1.add(jbedit); panel1.add(btnpaiipaii); panel2 = new jpanel (); panel2.setlayout(null); lbladdmatauang.setbounds(20, 20, 100, 30); jtfaddmatauang.setbounds(150, 20, 150, 30); lbladdharga.setbounds(20, 100, 100, 30); jtfaddharga.setbounds(150, 100, 150, 30); btnadd.setbounds(100, 300, 100, 30); panel2.add(lbladdmatauang); panel2.add(jtfaddmatauang); panel2.add(lbladdharga); panel2.add(jtfaddharga); panel2.add(btnadd); panel3 = new jpanel(); panel3.setlayout(null); showcombobox(); string daftarmatauang[] = shasa1.toarray(new string[shasa1.size()]); jcombobox matauang = new jcombobox(daftarmatauang); matauang.addactionlistener(this); matauang.setbounds(20, 20, 300, 30); lbldeletemu.setbounds(20, 50, 250, 30); btndelete.setbounds(150, 250, 100, 30); panel3.add(matauang); panel3.add(lbldeletemu); panel3.add(btndelete); tabpane.addtab ("update/perubahan", null, panel1); tabpane.addtab("add", null,panel2); tabpane.addtab("delete", null,panel3); getcontentpane().add(tabpane); } public void resettable() { (int = 0; < table.getrowcount(); i++) { (int j = 0; j < table.getcolumncount(); j++) { table.setvalueat(null, i, j); } } refreshtable(); } public void showcombobox() { conn.connect(); string query = "select mata_uang nilai_tukar"; try { statement stmt = conn.con.createstatement(); resultset rs = stmt.executequery(query); while(rs.next()) { shasa1.add(rs.getstring("mata_uang")); } } catch(exception e1) { e1.printstacktrace(); } conn.disconnect(); } public void tampil() { try { int row = tbm.getrowcount(); tbm.addrow(judulkolom); connectionmanager conn = new connectionmanager(); conn.connect(); statement stmt = conn.con.createstatement(); resultset rs = stmt.executequery("select * nilai_tukar"); int no=0; while(rs.next()) { no++; string no_kamar = rs.getstring("mata_uang"); double harga = rs.getdouble("harga"); data[no][0] = no_kamar; data[no][1] = harga; tbm.addrow(data[no]); row++; } rs.close(); conn.disconnect(); } catch(exception exc) { exc.printstacktrace(); } } public void refreshtable(){ table.setvalueat("mata uang", row, 0); table.setvalueat("harga", row, 1); row++; try { class.forname(driver); connection conns = drivermanager.getconnection(url, username, password); preparedstatement query = (preparedstatement) conns.preparestatement("select * nilai_tukar"); resultset rs = query.executequery(); while (rs.next()) { table.setvalueat(rs.getstring("mata_uang"), row, 0); table.setvalueat(rs.getdouble("harga"), row, 1); row++; } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public void actionperformed(actionevent e) { if(e.getsource()==jbedit) { string mu ; double temp =0; mu = jtfmatauang.gettext(); temp = double.parsedouble(jtfeditharga.gettext()); try { connectionmanager conn = new connectionmanager(); conn.connect(); string query ="update nilai_tukar set harga="+temp+" mata_uang='"+mu+"'"; statement stmt1 = conn.con.createstatement(); int updatefield = stmt1.executeupdate(query); joptionpane.showmessagedialog(null, "data anda terupdate !!"); resettable(); } catch (exception e2) { e2.printstacktrace(); } } if(e.getsource()==btnadd) { connectionmanager conn = new connectionmanager(); conn.connect(); if(!jtfaddharga.gettext().equals("") && !jtfaddmatauang.gettext().equals("")) { double h = double.parsedouble(jtfaddharga.gettext()) ; string mu = jtfaddmatauang.gettext(); string query = "insert nilai_tukar values(?,?)"; try { preparedstatement stmt = (preparedstatement) conn.con.preparestatement(query); stmt.setstring(1, mu); stmt.setdouble(2, h); stmt.executeupdate(); joptionpane.showmessagedialog(null, "new data added succecfully"); table.repaint(); } catch(exception e1){ e1.printstacktrace(); } conn.disconnect(); } else { joptionpane.showmessagedialog(null, "data harus diisi"); } } if(e.getsource() == btnpaiipaii) { dispose(); new gui(); } try { jcombobox<string> test = (jcombobox)e.getsource(); ket = (string) test.getselecteditem(); lbldeletemu.settext("delete mata uang : " + ket); } catch (exception e2) { } if(e.getsource() == btndelete) { connectionmanager conn = new connectionmanager(); conn.connect(); system.out.println(ket); string sql = "delete nilai_tukar mata_uang = '"+ket+"'"; try { preparedstatement stmt = (preparedstatement) conn.con.preparestatement(sql); stmt.executeupdate(); system.out.println(ket); joptionpane.showmessagedialog(null, "data deleted succesfully"); conn.connect(); string query2 = "select mata_uang nilai_tukar"; try { statement stmt2 = conn.con.createstatement(); resultset rs2 = stmt2.executequery(query2); while(rs2.next()) { system.out.println(rs2.getstring("mata_uang")); shasa1.add(rs2.getstring("mata_uang")); } } catch(exception e1) { e1.printstacktrace(); } conn.disconnect(); showcombobox(); string daftarmatauang[] = shasa1.toarray(new string[shasa1.size()]); jcombobox matauang = new jcombobox(daftarmatauang); matauang.addactionlistener(this); matauang.setbounds(20, 20, 300, 30); panel3.add(matauang); } catch(exception e1){ e1.printstacktrace(); } conn.disconnect(); } } @override public void valuechanged(listselectionevent e) { int maxrows; int[] selrows; object value; if (!e.getvalueisadjusting()) { selrows = table.getselectedrows(); if (selrows.length > 0) { jtfmatauang.settext((string) data[table.getselectedrow()][0]); jtfeditharga.settext(data[table.getselectedrow()][1].tostring()); } } } public static void main(string[] args) { new cek(); } }
Comments
Post a Comment