i have gui hasjcheckbox
in jtable
table populated data database of vector.
i providing code below extracting data database:
try { preparedstatement pre = con.preparestatement("select * productinformation customername='"+cust_name+"'"); vector<vector<string>> historyvector = new vector<vector<string>>(); rs = pre.executequery(); while(rs.next()) { vector<string> history = new vector<string>(); serial_no=rs.getstring("serialnumber"); history.add(serial_no); history.add(rs.getstring("model")); history.add(rs.getstring("invoicenumber")); history.add(rs.getstring("location")); historyvector.add(history); } data=historyvector; header = new vector<string>(); header.add("serial no"); header.add("model"); header.add("invoice no"); header.add("location"); header.add("asc"); header.add("amc"); amcascresult(); mainframe.add(amcascresult); mainframe.remove(amc); mainframe.validate(); mainframe.repaint(); mainframe.pack(); } catch(exception e){ system.err.println(e); }
i experiencing problem when run program select jcheckbox
returns null pointer exception. times runs expected.
my code inserting records table
amcasc_table.setmodel(new javax.swing.table.defaulttablemodel( data,header ) { boolean[] canedit = new boolean [] { false, false, false, true, true }; private static final long serialversionuid = 1l; /*@override public class getcolumnclass(int column) { return getvalueat(0, column).getclass(); }*/ @override public class getcolumnclass(int column) { switch (column) { case 0: return string.class; case 1: return string.class; case 2: return string.class; case 3: return string.class; default: return boolean.class; } } public boolean iscelleditable(int rowindex, int columnindex) { return canedit [columnindex]; } }); jscrollpane1.setviewportview(amcasc_table);
and getting values of jcheckbox
.
(int = 0; <amcasc_table.getrowcount(); i++) { boolean ischecked = (boolean) amcasc_table.getvalueat(i, 4); if (ischecked) { //get values of columns need. string value = (string) amcasc_table.getvalueat(i, 0); system.out.println(value); } }
what mistake have done , can me how overcome this?
sscce
import java.awt.headlessexception; import java.awt.event.actionevent; import java.awt.event.keyevent; import java.sql.*; import java.text.dateformat; import java.text.format; import java.text.parseexception; import java.text.simpledateformat; import java.util.date; import java.util.properties; import java.util.vector; import javax.mail.*; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; import javax.swing.*; public class combined { session session; jframe mainframe,mailframe; jpanel firstpane, newsales, confirmnewsales, checkserialno, producthistory, newcomplaint, newcomplaintentry; jpanel confirmcomplaint,sendmail,generatereport,generatereportresult,amc,amcascresult,amcascconfirm; jmenubar menubar; jmenu menu; jmenuitem menuitem; jmenu submenu; jradiobuttonmenuitem rbmenuitem; jcheckboxmenuitem cbmenuitem; connection con,con1; statement st,st1; dateformat dateformat = new simpledateformat("dd/mm/yyyy"); format mmformat = new simpledateformat("mm"); dateformat yyformat = new simpledateformat("yy"); dateformat ddformat = new simpledateformat("dd"); string invoice_no; string serial_no; string model; string cust_name; string cust_location; string amc_status; date date_of_purchase; date amc_start_date; date amc_end_date; string amcend_date; string amcstart_date; buttonmodel amc_status_model; string query; resultset rs,rs1; date dateofpurchase; string date_ofpurchase; string query1; string eng_name; string problem_description; string contact_person; string contact_no; string calldate; string callmonth; string callyear; string callno; int inccallno; string lastcallno; string query3, query4, query5; string lastcalldate; string sinccallno; string db; string toaddress; string subject; string messagebody; string warrantystatus; vector<vector<string>> data; vector<string> header; string callstatus; int no_of_calls = 0; public combined() { connect(); framegen(); } private void framegen() { mainframe = new jframe("product maintenance , support software"); //create menu bar. menubar = new jmenubar(); //build first menu. menu = new jmenu("a menu"); menu.setmnemonic(keyevent.vk_a); menu.getaccessiblecontext().setaccessibledescription( "the menu in program has menu items"); menubar.add(menu); //a group of jmenuitems menuitem = new jmenuitem("a text-only menu item", keyevent.vk_t); menuitem.setaccelerator(keystroke.getkeystroke( keyevent.vk_1, actionevent.alt_mask)); menuitem.getaccessiblecontext().setaccessibledescription( "this doesn't anything"); menu.add(menuitem); menuitem = new jmenuitem("both text , icon", new imageicon("images/middle.gif")); menuitem.setmnemonic(keyevent.vk_b); menu.add(menuitem); menuitem = new jmenuitem(new imageicon("images/middle.gif")); menuitem.setmnemonic(keyevent.vk_d); menu.add(menuitem); //a group of radio button menu items menu.addseparator(); buttongroup group = new buttongroup(); rbmenuitem = new jradiobuttonmenuitem("a radio button menu item"); rbmenuitem.setselected(true); rbmenuitem.setmnemonic(keyevent.vk_r); group.add(rbmenuitem); menu.add(rbmenuitem); rbmenuitem = new jradiobuttonmenuitem("another one"); rbmenuitem.setmnemonic(keyevent.vk_o); group.add(rbmenuitem); menu.add(rbmenuitem); //a group of check box menu items menu.addseparator(); cbmenuitem = new jcheckboxmenuitem("a check box menu item"); cbmenuitem.setmnemonic(keyevent.vk_c); menu.add(cbmenuitem); cbmenuitem = new jcheckboxmenuitem("another one"); cbmenuitem.setmnemonic(keyevent.vk_h); menu.add(cbmenuitem); //a submenu menu.addseparator(); submenu = new jmenu("a submenu"); submenu.setmnemonic(keyevent.vk_s); menuitem = new jmenuitem("an item in submenu"); menuitem.setaccelerator(keystroke.getkeystroke( keyevent.vk_2, actionevent.alt_mask)); submenu.add(menuitem); menuitem = new jmenuitem("another item"); submenu.add(menuitem); menu.add(submenu); //build second menu in menu bar. menu = new jmenu("another menu"); menu.setmnemonic(keyevent.vk_n); menu.getaccessiblecontext().setaccessibledescription( "this menu nothing"); menubar.add(menu); mainframe.setjmenubar(menubar); firstpane(); mainframe.add(firstpane); mainframe.pack(); mainframe.setvisible(true); mainframe.setdefaultcloseoperation(3); mainframe.setresizable(false); } /** * @param args command line arguments */ public static void main(string[] args) { // todo code application logic here combined com = new combined(); } private void connect() { try { string driver = "sun.jdbc.odbc.jdbcodbcdriver"; class.forname(driver); string db1 = "jdbc:odbc:solaris"; con = drivermanager.getconnection(db1); st = con.createstatement(); } catch (classnotfoundexception | sqlexception ex) { system.out.println("database connection failed"); } } private void firstpane() { firstpane = new jpanel(); addnewsales_button = new javax.swing.jbutton(); newcomplaint_button = new javax.swing.jbutton(); calllist_button = new javax.swing.jbutton(); generatereport_button = new javax.swing.jbutton(); serialnocheck_button = new javax.swing.jbutton(); solaris_logo = new javax.swing.jlabel(); support_label = new javax.swing.jlabel(); putamc_button = new javax.swing.jbutton(); firstpane.setbackground(new java.awt.color(255, 255, 255)); addnewsales_button.settext("add new sales entry"); addnewsales_button.addactionlistener(new java.awt.event.actionlistener() { @override public void actionperformed(java.awt.event.actionevent evt) { // addnewsales_buttonactionperformed(evt); } }); newcomplaint_button.settext("new complaint"); newcomplaint_button.addactionlistener(new java.awt.event.actionlistener() { @override public void actionperformed(java.awt.event.actionevent evt) { // newcomplaint_buttonactionperformed(evt); } }); calllist_button.settext("call list"); calllist_button.addactionlistener(new java.awt.event.actionlistener() { @override public void actionperformed(java.awt.event.actionevent evt) { // calllist_buttonactionperformed(evt); } }); generatereport_button.settext("generate report"); generatereport_button.addactionlistener(new java.awt.event.actionlistener() { @override public void actionperformed(java.awt.event.actionevent evt) { //generatereport_buttonactionperformed(evt); } }); serialnocheck_button.settext("serial number check"); serialnocheck_button.addactionlistener(new java.awt.event.actionlistener() { @override public void actionperformed(java.awt.event.actionevent evt) { // serialnocheck_buttonactionperformed(evt); } }); solaris_logo.seticon(new javax.swing.imageicon(getclass().getresource("/combined/image/solaris_logo.jpg"))); // noi18n support_label.settext("support section"); putamc_button.settext("put amc"); putamc_button.addactionlistener(new java.awt.event.actionlistener() { @override public void actionperformed(java.awt.event.actionevent evt) { putamc_buttonactionperformed(evt); } }); javax.swing.grouplayout layout = new javax.swing.grouplayout(firstpane); firstpane.setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading).addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup().addcontainergap(javax.swing.grouplayout.default_size, short.max_value).addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading).addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup().addcomponent(calllist_button).addgap(41, 41, 41).addcomponent(putamc_button).addgap(28, 28, 28).addcomponent(generatereport_button).addgap(74, 74, 74)).addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup().addcomponent(solaris_logo).addgap(132, 132, 132)))).addgroup(layout.createsequentialgroup().addgap(20, 20, 20).addcomponent(addnewsales_button).addgap(44, 44, 44).addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading).addgroup(layout.createsequentialgroup().addcomponent(support_label, javax.swing.grouplayout.preferred_size, 130, javax.swing.grouplayout.preferred_size).addcontainergap()).addgroup(layout.createsequentialgroup().addcomponent(serialnocheck_button).addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 43, short.max_value).addcomponent(newcomplaint_button).addgap(23, 23, 23))))); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading).addgroup(layout.createsequentialgroup().addgap(32, 32, 32).addcomponent(solaris_logo, javax.swing.grouplayout.preferred_size, 47, javax.swing.grouplayout.preferred_size).addpreferredgap(javax.swing.layoutstyle.componentplacement.related).addcomponent(support_label, javax.swing.grouplayout.preferred_size, 35, javax.swing.grouplayout.preferred_size).addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 72, short.max_value).addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline).addcomponent(addnewsales_button).addcomponent(serialnocheck_button).addcomponent(newcomplaint_button)).addgap(28, 28, 28).addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline).addcomponent(calllist_button).addcomponent(generatereport_button).addcomponent(putamc_button)).addgap(34, 34, 34))); } private void putamc_buttonactionperformed(java.awt.event.actionevent evt) { // todo add handling code here: amc(); mainframe.add(amc); mainframe.remove(firstpane); mainframe.validate(); mainframe.repaint(); mainframe.pack(); } private void amc() { amc=new jpanel(); custname_label = new javax.swing.jlabel(); custname_txtbox = new javax.swing.jtextfield(); amc_search_button = new javax.swing.jbutton(); amc.setborder(javax.swing.borderfactory.createtitledborder(null, "amc sign up", javax.swing.border.titledborder.default_justification, javax.swing.border.titledborder.default_position, null, new java.awt.color(0, 0, 0))); custname_label.settext("enter customer name"); amc_search_button.settext("search"); amc_search_button.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { amc_search_buttonactionperformed(evt); } }); javax.swing.grouplayout layout = new javax.swing.grouplayout(amc); amc.setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcomponent(custname_label) .addgap(18, 18, 18) .addcomponent(custname_txtbox, javax.swing.grouplayout.preferred_size, 173, javax.swing.grouplayout.preferred_size) .addgap(0, 46, short.max_value)) .addgroup(layout.createsequentialgroup() .addgap(116, 116, 116) .addcomponent(amc_search_button) .addcontainergap()) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(46, 46, 46) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(custname_label) .addcomponent(custname_txtbox, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addgap(51, 51, 51) .addcomponent(amc_search_button) .addcontainergap(136, short.max_value)) ); } private void amc_search_buttonactionperformed(java.awt.event.actionevent evt) { // todo add handling code here: cust_name=custname_txtbox.gettext().trim(); try { preparedstatement pre = con.preparestatement("select * productinformation customername='"+cust_name+"'"); vector<vector<string>> historyvector = new vector<vector<string>>(); rs = pre.executequery(); while(rs.next()) { vector<string> history = new vector<string>(); serial_no=rs.getstring("serialnumber"); system.out.println("here");//empid history.add(serial_no); history.add(rs.getstring("model")); history.add(rs.getstring("invoicenumber")); history.add(rs.getstring("location")); historyvector.add(history); } data=historyvector; header = new vector<string>(); header.add("serial no"); header.add("model"); header.add("invoice no"); header.add("location"); header.add("asc"); header.add("amc"); amcascresult(); mainframe.add(amcascresult); mainframe.remove(amc); mainframe.validate(); mainframe.repaint(); mainframe.pack(); } catch(exception e){ system.err.println(e); } } private void amcascresult() { amcascresult=new jpanel(); custname_label = new javax.swing.jlabel(); custname_var_label = new javax.swing.jlabel(); location_label = new javax.swing.jlabel(); custlocation_var_label = new javax.swing.jlabel(); jscrollpane1 = new javax.swing.jscrollpane(); amcasc_table = new javax.swing.jtable(); putamcasc_button = new javax.swing.jbutton(); amcascresult.setborder(javax.swing.borderfactory.createtitledborder("put amc")); custname_label.settext("customer name :"); custname_var_label.settext(cust_name); location_label.settext("location :"); custlocation_var_label.settext("<location>"); amcasc_table.setmodel(new javax.swing.table.defaulttablemodel( data,header ) { boolean[] canedit = new boolean [] { false, false, false, true, true }; private static final long serialversionuid = 1l; /*@override public class getcolumnclass(int column) { return getvalueat(0, column).getclass(); }*/ @override public class getcolumnclass(int column) { switch (column) { case 0: return string.class; case 1: return string.class; case 2: return string.class; case 3: return string.class; default: return boolean.class; } } public boolean iscelleditable(int rowindex, int columnindex) { return canedit [columnindex]; } }); jscrollpane1.setviewportview(amcasc_table); putamcasc_button.settext("put amc/asc"); putamcasc_button.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { putamcasc_buttonactionperformed(evt); } }); javax.swing.grouplayout layout = new javax.swing.grouplayout(amcascresult); amcascresult.setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing) .addcomponent(putamcasc_button) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcomponent(custname_label) .addpreferredgap(javax.swing.layoutstyle.componentplacement.unrelated) .addcomponent(custname_var_label) .addgap(18, 18, 18) .addcomponent(location_label) .addgap(18, 18, 18) .addcomponent(custlocation_var_label)) .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 375, javax.swing.grouplayout.preferred_size))) .addcontainergap(javax.swing.grouplayout.default_size, short.max_value)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(custname_label) .addcomponent(custname_var_label) .addcomponent(location_label) .addcomponent(custlocation_var_label)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.unrelated) .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 213, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 10, short.max_value) .addcomponent(putamcasc_button)) ); } private void putamcasc_buttonactionperformed(java.awt.event.actionevent evt) { // todo add handling code here: (int = 0; <amcasc_table.getrowcount(); i++) { boolean ischecked = (boolean) amcasc_table.getvalueat(i, 4); if (ischecked) { //get values of columns need. string value = (string) amcasc_table.getvalueat(i, 0); system.out.println(value); } } amcascconfirm(); mainframe.add(amcascconfirm); mainframe.remove(amcascresult); mainframe.validate(); mainframe.repaint(); mainframe.pack(); } private void amcascconfirm() { amcascconfirm=new jpanel(); confirm_label = new javax.swing.jlabel(); finish_button = new javax.swing.jbutton(); amcascconfirm.setborder(javax.swing.borderfactory.createtitledborder("confirmation of amc/asc")); confirm_label.settext("amc/asc has been availed machines selected"); finish_button.settext("finish"); finish_button.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { finish_button1actionperformed(evt); } }); javax.swing.grouplayout layout = new javax.swing.grouplayout(amcascconfirm); amcascconfirm.setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(73, 73, 73) .addcomponent(confirm_label) .addgap(0, 95, short.max_value)) .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() .addcontainergap(javax.swing.grouplayout.default_size, short.max_value) .addcomponent(finish_button))) .addcontainergap()) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(59, 59, 59) .addcomponent(confirm_label) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 169, short.max_value) .addcomponent(finish_button) .addcontainergap()) ); } private void finish_button1actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: mainframe.add(firstpane); mainframe.remove(amcascconfirm); mainframe.validate(); mainframe.repaint(); mainframe.pack(); } private javax.swing.jlabel confirm_label; private javax.swing.jbutton finish_button; private javax.swing.jtable amcasc_table; private javax.swing.jlabel custlocation_var_label; private javax.swing.jlabel custname_label; private javax.swing.jlabel custname_var_label; private javax.swing.jscrollpane jscrollpane1; private javax.swing.jlabel location_label; private javax.swing.jbutton putamcasc_button; private javax.swing.jbutton amc_search_button; private javax.swing.jtextfield custname_txtbox; private javax.swing.jlabel support_label; private javax.swing.jbutton addnewsales_button; private javax.swing.jbutton calllist_button; private javax.swing.jbutton generatereport_button; private javax.swing.jbutton newcomplaint_button; private javax.swing.jbutton putamc_button; private javax.swing.jbutton serialnocheck_button; private javax.swing.jlabel solaris_logo; }
among 700+ lines of incomplete example, several issues stand out:
the word sometimes suggests incorrect synchronization; verify swing gui objects constructed , manipulated only on event dispatch thread.
ensure
tablemodel
returnsboolean.class
getcolumnclass()
checkbox column, rather merelydefault
; complete examples usingdefaulttablemodel
may seen here , here.don't replace components, e.g.
amc
in first fragment ,amcascconfirm
in magnum opus; update corresponding model of contained component.verify correct query results independently of program;
select *
particularly troublesome if schema mutates.
Comments
Post a Comment