swing - Java how to refresh sql outcome in jlist -
i'm having little problem java application. want show orders of restaurant in left jlist, , details of selected order in right jlist. orders read in through mysql query , printed jlist. however, want make refresh button whitch query has executed again , printed jlist. however, can not make refresh button work. refresh content, opens new jfram window...
from gui class:
public class barscherm extends javax.swing.jframe { private bedieningsmanager manager; /** * creates new form barscherm */ public barscherm() { manager = new bedieningsmanager(); initcomponents(); } private void opvraagknopactionperformed(java.awt.event.actionevent evt) { // refresh button jpanel1.removeall(); java.awt.eventqueue.invokelater(new runnable() { public void run() { new barscherm().setvisible(true); } }); }
from manager class:
public class bedieningsmanager { private arraylist<barbestelling> bestellingen; private int tafelnummer,bestelnummer, aantal; database db1 = new database(); public bedieningsmanager(){ this.bestellingen = new arraylist<barbestelling>(); maakbestellingen(); public void maakbestellingen(){ aantal = db1.getaantalrijenbarbestelling(); barbestelling[] bss = new barbestelling[aantal]; (int = 0; < aantal; i++){ tafelnummer = db1.gettafelnummer(i); bestelnummer = db1.getbestelnummer(i); bss[i] = new barbestelling(bestelnummer,tafelnummer,"geplaatst"); bestellingen.add(bss[i]); } } public arraylist<string> toonbarbestellingen() { arraylist<string> tafelnummers = new arraylist<string>(); for(barbestelling : bestellingen) { tafelnummers.add(i.gettafelnummer()); } return tafelnummers; } public arraylist<barbestelling> getbestellingen() { return bestellingen; } public string getdetails(string barbestelling){ string details = ""; boolean found = false; int count = 0; while(!found && count < bestellingen.size()) { if(bestellingen.get(count).gettafelnummer().equals(barbestelling)) { found = true; details = bestellingen.get(count).getdetails(); } else { count ++; } } if(!found) { system.out.println("bestelling met tafelnummer: " + barbestelling + " bestaat niet!"); } return details; } }
the database query's:
public class database { private connection con; private int bestelnummer; public database() { string connectiestring; connectiestring = "jdbc:mysql://localhost:3306/hartigehap"; try { con = drivermanager.getconnection(connectiestring,"root", ""); } catch(exception e) { system.err.println("melding: " + e.getmessage()); } } public int getaantalrijenbestelregel(int bestelnummer){ statement stmt; resultset rs; string query; int = 0; try { stmt = con.createstatement(); query = "select count(itemitemid) bestelregel barbestellingbestellingnummer = '"+bestelnummer+"'"; rs = stmt.executequery(query); while (rs.next()){ = rs.getint("count(itemitemid)"); } } catch(exception e) { system.err.println("melding1: " + e.getmessage()); } return ; } public int getaantalrijenbarbestelling(){ statement stmt; resultset rs; string query; int = 0; try { stmt = con.createstatement(); query = "select bestellingnummer barbestelling status = 'geplaatst'"; rs = stmt.executequery(query); while ( rs.next() ) { i++; } stmt.close(); } catch(exception e) { system.err.println("melding1: " + e.getmessage()); } return ; } public string getproduct(int bestelnummer){ statement stmt; resultset rs; string query; string productnaam = ""; try { stmt = con.createstatement(); query = "select naam item, bestelregel bestelregel.barbestellingbestellingnummer = "+bestelnummer+" , item.itemid = bestelregel.itemitemid"; rs = stmt.executequery(query); rs.absolute(bestelnummer); productnaam = rs.getstring("naam"); } catch(exception e) { system.err.println("melding1: " + e.getmessage()); } return productnaam; } public int gettafelnummer(int bestelnummer){ statement stmt; resultset rs; string query; int tafelnummer = 0; bestelnummer++; try { stmt = con.createstatement(); query = "select barbestelling.tafelnummer barbestelling barbestelling.bestellingnummer = "+bestelnummer+""; rs = stmt.executequery(query); rs.absolute(1); tafelnummer = rs.getint("tafelnummer"); } catch(exception e) { system.err.println("melding1: " + e.getmessage()); } return tafelnummer; } public int getaantal(int bestelnummer){ statement stmt; resultset rs; string query; int aantal = 0; try { stmt = con.createstatement(); query = "select aantal bestelregel barbestellingbestellingnummer = '"+bestelnummer+"'"; rs = stmt.executequery(query); rs.absolute(bestelnummer); aantal = rs.getint("aantal"); } catch(exception e) { system.err.println("melding1: " + e.getmessage()); } return aantal; } public string getbestelregel(int bestelnummer) { statement stmt; resultset rs; string query, querytekst1; string details = ""; try { stmt = con.createstatement(); query = "select bestelregel.aantal, item.naam bestelregel, item bestelregel.barbestellingbestellingnummer = " + bestelnummer + " , item.itemid = bestelregel.itemitemid"; rs = stmt.executequery(query); while(rs.next()){ string aantal = rs.getstring("aantal"); string naam = rs.getstring("naam"); details += aantal + " " + naam + "\n"; } } catch(exception e) { system.err.println("melding1: " + e.getmessage()); } return details; } public int getbestelnummer(int bestelnummer) { statement stmt; resultset rs; string query; bestelnummer++; try { stmt = con.createstatement(); query = "select bestellingnummer barbestelling bestellingnummer = '"+bestelnummer+"' , status = 'geplaatst'"; rs = stmt.executequery(query); rs.absolute(1); bestelnummer = rs.getint("bestellingnummer"); } catch(exception e) { system.err.println("melding1: " + e.getmessage()); } return bestelnummer; } private boolean sluitconnectie() { boolean x = false; try { if(con != null) { con.close(); x = true; } } catch(sqlexception e) { x = false; } return x; } }
a screenshot of gui:
use jlist.setmodel set own instance of defaultlistmodel. has methods adding, changing , removing content, , jlist should monitor observing changes.
Comments
Post a Comment