java - How do I put multiple JPanels (3) in a JPanel Using JTabbedPane? -
i trying create gui app using jtabbedpane create 5 tabs connected database. each tab contains panel 3 panels in (one panel hold jbuttons hold jlabels , jtextfields , last hold jtable. each south, center , north respectively). after creating tabs , components, last tab displays , displays incorrectly (it displays jlabels, jtextfields , jbuttons of 5 tabs). if remove tabs , leave 1 displays correctly if more one, displays on last tab. don't know how resolve it. please me.
the source code bit lengthy please bear me , me check it. if there better way write code please let me know.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.event.actionlistener; import java.sql.*; import java.util.vector; public class project_salesdatabase extends jframe { jtabbedpane tabbedpane = new jtabbedpane(); imageicon icon = createimageicon("images/middle.gif"); jbutton view = new jbutton(" view "); jbutton save = new jbutton(" save "); jbutton addnew = new jbutton(" add new "); jbutton exit = new jbutton(" exit application "); jpanel displaybiscuitspanel = new jpanel(); jpanel displaycookingpanel = new jpanel(); jpanel displaycustomerspanel = new jpanel(); jpanel displayemployeespanel = new jpanel(); jpanel displayprovisionspanel = new jpanel(); jpanel displaybutton = new jpanel(); jpanel displaycontent = new jpanel((new gridlayout(10, 2))); jpanel displaytable = new jpanel(new gridlayout(1, 2)); jtextfield biscuitname = new jtextfield(); jtextfield biscuitprice = new jtextfield(); jtextfield biscuitcompany = new jtextfield(); jtextfield quantityofbiscuitsbought = new jtextfield(); jtextfield quantityofbiscuitssold = new jtextfield(); jtextfield quantityinstock = new jtextfield(); jtextfield itemname = new jtextfield(); jtextfield itemprice = new jtextfield(); jtextfield itemtype = new jtextfield(); jtextfield quantityofitemsbought = new jtextfield(); jtextfield quantityofitemssold = new jtextfield(); jtextfield firstname = new jtextfield(); jtextfield lastname = new jtextfield(); jtextfield customerattendant = new jtextfield(); jtextfield customerattendantposition = new jtextfield(); jtextfield iscustomeradebtor = new jtextfield(); jtextfield ordernumber = new jtextfield(); jtextfield debtamount = new jtextfield(); jtextfield address = new jtextfield(); jtextfield phonenumber = new jtextfield(); jtextfield position = new jtextfield(); jtextfield age = new jtextfield(); jtextfield salary = new jtextfield(); jtextfield nextofkin = new jtextfield(); jtextfield relationshipwithnextofkin = new jtextfield(); jtextfield nextofkinphonenumber = new jtextfield(); jtextfield itemcompany = new jtextfield(); public static void main(string[] args) { project_salesdatabase mainframe = new project_salesdatabase(); mainframe.setlocationrelativeto(null); mainframe.setvisible(true); mainframe.setdefaultcloseoperation(jframe.exit_on_close); } public project_salesdatabase() { settitle(" database app "); setsize(1000, 500); // create tab pages biscuitstable(); cookingingredientstable(); customerstable(); employeestable(); provisionstable(); jpanel toppanel = new jpanel(); toppanel.setlayout(new gridlayout(1, 3)); getcontentpane().add(toppanel); toppanel.add(tabbedpane, borderlayout.center); // create tabs in tabbedpane tabbedpane.addtab("biscuits database", icon, displaybiscuitspanel, "allows view or enter data biscuits database"); tabbedpane.addtab("cooking ingredients database", icon, displaycookingpanel, "allows view or enter data cooking ingredients database"); tabbedpane.addtab("customers database", icon, displaycustomerspanel, "allows view or enter data customers database"); tabbedpane.addtab("employees database", icon, displayemployeespanel, "allows view or enter data employees database"); tabbedpane.addtab("provisions database", icon, displayprovisionspanel, "allows view or enter data provisions database"); //enable scrolling in tabs. tabbedpane.settablayoutpolicy(jtabbedpane.scroll_tab_layout); } public static imageicon createimageicon(string path) { java.net.url imgurl = project_salesdatabase.class.getresource(path); if (imgurl != null) { return new imageicon(imgurl); } else { system.err.println("couldn't find file: " + path); return null; } } public final void biscuitstable() { displaybiscuitspanel.setlayout(new borderlayout()); displaybiscuitspanel.add(displaycontent, borderlayout.center); displaybiscuitspanel.add(displaybutton, borderlayout.south); displaybiscuitspanel.add(displaytable, borderlayout.north); displaycontent.add(new jlabel("biscuit name")); displaycontent.add(biscuitname); displaycontent.add(new jlabel("biscuit price")); displaycontent.add(biscuitprice); displaycontent.add(new jlabel("biscuit company")); displaycontent.add(biscuitcompany); displaycontent.add(new jlabel("quantity of biscuits bought")); displaycontent.add(quantityofbiscuitsbought); displaycontent.add(new jlabel("quantity of biscuits sold")); displaycontent.add(quantityofbiscuitssold); displaycontent.add(new jlabel("quantity in stock")); displaycontent.add(quantityinstock); displaybutton.add(addnew); displaybutton.add(save); displaybutton.add(view); displaybutton.add(exit); view.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string db_url = "jdbc:odbc:projectsalesdatabase"; string username = ""; string password = ""; connection con = null; try { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); con = drivermanager.getconnection(db_url, username, password); } catch (classnotfoundexception | sqlexception f) { joptionpane.showmessagedialog(rootpane, f.getmessage()); } statement state = null; resultset set = null; try { string query = "select * biscuits"; state = con.createstatement(); set = state.executequery(query); boolean nextrec = set.next(); if (!nextrec) { joptionpane.showmessagedialog(rootpane, "no record"); } else { vector col = new vector(); vector row = new vector(); resultsetmetadata rsm = set.getmetadata(); (int x = 1; x <= rsm.getcolumncount(); x++) { col.addelement(rsm.getcolumnname(x)); } { row.addelement(getnextrow(set, rsm)); } while (set.next()); jtable tab = new jtable(row, col); displaycontent.removeall(); displaytable.removeall(); displaytable.add(new jscrollpane(tab), borderlayout.center); validate(); } state.close(); } catch (sqlexception sql) { joptionpane.showmessagedialog(rootpane, sql.getmessage()); } } }); save.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { try { string query = "insert biscuits values ('" + biscuitname.gettext() + "'," + " '" + biscuitprice.gettext() + "'," + " '" + biscuitcompany.gettext() + "'," + " '" + quantityofbiscuitsbought.gettext() + "'," + " '" + quantityofbiscuitssold.gettext() + "'," + " '" + quantityinstock.gettext() + "')"; string db_url = "jdbc:odbc:projectsalesdatabase"; string username = ""; string password = ""; connection con = null; try { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); con = drivermanager.getconnection(db_url, username, password); } catch (classnotfoundexception | sqlexception f) { joptionpane.showmessagedialog(rootpane, f.getmessage()); } statement state = con.createstatement(); int rep = state.executeupdate(query); if (rep == 0) { joptionpane.showmessagedialog(rootpane, "no data saved"); } else { joptionpane.showmessagedialog(rootpane, "data saved"); } } catch (sqlexception sqle) { sqle.getmessage(); } } }); addnew.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { try { biscuitname.settext(""); biscuitprice.settext(""); biscuitcompany.settext(""); quantityofbiscuitsbought.settext(""); quantityofbiscuitssold.settext(""); quantityinstock.settext(""); displaytable.removeall(); displaycontent.removeall(); displaycontent.add(new jlabel("biscuit name")); displaycontent.add(biscuitname); displaycontent.add(new jlabel("biscuit price")); displaycontent.add(biscuitprice); displaycontent.add(new jlabel("biscuit company")); displaycontent.add(biscuitcompany); displaycontent.add(new jlabel("quantity of biscuits bought")); displaycontent.add(quantityofbiscuitsbought); displaycontent.add(new jlabel("quantity of biscuits sold")); displaycontent.add(quantityofbiscuitssold); displaycontent.add(new jlabel("quantity in stock")); displaycontent.add(quantityinstock); validate(); } catch (exception f) { f.getmessage(); } } }); exit.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { system.exit(0); } }); } public final void cookingingredientstable() { displaycookingpanel.setlayout(new borderlayout()); displaycookingpanel.add(displaycontent, borderlayout.center); displaycookingpanel.add(displaybutton, borderlayout.south); displaycookingpanel.add(displaytable, borderlayout.north); displaycontent.add(new jlabel("item name")); displaycontent.add(itemname); displaycontent.add(new jlabel("item price")); displaycontent.add(itemprice); displaycontent.add(new jlabel("item type")); displaycontent.add(itemtype); displaycontent.add(new jlabel("quantity of items bought")); displaycontent.add(quantityofitemsbought); displaycontent.add(new jlabel("quantity of items sold")); displaycontent.add(quantityofitemssold); displaycontent.add(new jlabel("quantity in stock")); displaycontent.add(quantityinstock); displaybutton.add(addnew); displaybutton.add(save); displaybutton.add(view); displaybutton.add(exit); view.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string db_url = "jdbc:odbc:projectsalesdatabase"; string username = ""; string password = ""; connection con = null; try { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); con = drivermanager.getconnection(db_url, username, password); } catch (classnotfoundexception | sqlexception f) { joptionpane.showmessagedialog(rootpane, f.getmessage()); } statement state = null; resultset set = null; try { string query = "select * cookingingredients"; state = con.createstatement(); set = state.executequery(query); boolean nextrec = set.next(); if (!nextrec) { joptionpane.showmessagedialog(rootpane, "no record"); } else { vector col = new vector(); vector row = new vector(); resultsetmetadata rsm = set.getmetadata(); (int x = 1; x <= rsm.getcolumncount(); x++) { col.addelement(rsm.getcolumnname(x)); } { row.addelement(getnextrow(set, rsm)); } while (set.next()); jtable tab = new jtable(row, col); displaycontent.removeall(); displaytable.removeall(); displaytable.add(new jscrollpane(tab), borderlayout.center); validate(); } state.close(); } catch (sqlexception sql) { joptionpane.showmessagedialog(rootpane, sql.getmessage()); } } }); save.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { try { string query = "insert cookingingredients values ('" + itemname.gettext() + "'," + " '" + itemprice.gettext() + "'," + " '" + itemtype.gettext() + "'," + " '" + quantityofitemsbought.gettext() + "'," + " '" + quantityofitemssold.gettext() + "'," + " '" + quantityinstock.gettext() + "')"; string db_url = "jdbc:odbc:projectsalesdatabase"; string username = ""; string password = ""; connection con = null; try { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); con = drivermanager.getconnection(db_url, username, password); } catch (classnotfoundexception | sqlexception f) { joptionpane.showmessagedialog(rootpane, f.getmessage()); } statement state = con.createstatement(); int rep = state.executeupdate(query); if (rep == 0) { joptionpane.showmessagedialog(rootpane, "no data saved"); } else { joptionpane.showmessagedialog(rootpane, "data saved"); } } catch (sqlexception sqle) { sqle.getmessage(); } } }); addnew.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { try { itemname.settext(""); itemprice.settext(""); itemtype.settext(""); quantityofitemsbought.settext(""); quantityofbiscuitssold.settext(""); quantityofitemssold.settext(""); quantityinstock.settext(""); displaytable.removeall(); displaycontent.removeall(); displaycontent.add(new jlabel("item name")); displaycontent.add(itemname); displaycontent.add(new jlabel("item price")); displaycontent.add(itemprice); displaycontent.add(new jlabel("item type")); displaycontent.add(itemtype); displaycontent.add(new jlabel("quantity of items bought")); displaycontent.add(quantityofitemsbought); displaycontent.add(new jlabel("quantity of items sold")); displaycontent.add(quantityofitemssold); displaycontent.add(new jlabel("quantity in stock")); displaycontent.add(quantityinstock); validate(); } catch (exception f) { f.getmessage(); } } }); } public final void customerstable() { displaycustomerspanel.setlayout(new borderlayout()); displaycustomerspanel.add(displaycontent, borderlayout.center); displaycustomerspanel.add(displaybutton, borderlayout.south); displaycustomerspanel.add(displaytable, borderlayout.north); displaycontent.add(new jlabel("first name")); displaycontent.add(firstname); displaycontent.add(new jlabel("last name")); displaycontent.add(lastname); displaycontent.add(new jlabel("customer attendant")); displaycontent.add(customerattendant); displaycontent.add(new jlabel("customer attendant's position")); displaycontent.add(customerattendantposition); displaycontent.add(new jlabel("is customer debtor?")); displaycontent.add(iscustomeradebtor); displaycontent.add(new jlabel("order number")); displaycontent.add(ordernumber); displaycontent.add(new jlabel("debt amount")); displaycontent.add(debtamount); displaycontent.add(new jlabel("customer's address")); displaycontent.add(address); displaycontent.add(new jlabel("customer's phone number")); displaycontent.add(phonenumber); displaybutton.add(addnew); displaybutton.add(save); displaybutton.add(view); displaybutton.add(exit); //button actions same other methods. removed because of body character limits } public final void employeestable() { displayemployeespanel.setlayout(new borderlayout()); displayemployeespanel.add(displaycontent, borderlayout.center); displayemployeespanel.add(displaybutton, borderlayout.south); displayemployeespanel.add(displaytable, borderlayout.north); displaycontent.add(new jlabel("first name")); displaycontent.add(firstname); displaycontent.add(new jlabel("last name")); displaycontent.add(lastname); displaycontent.add(new jlabel("position")); displaycontent.add(position); displaycontent.add(new jlabel("age")); displaycontent.add(age); displaycontent.add(new jlabel("salary")); displaycontent.add(salary); displaycontent.add(new jlabel("employees's address")); displaycontent.add(address); displaycontent.add(new jlabel("employees's phone number(s)")); displaycontent.add(phonenumber); displaycontent.add(new jlabel("next of kin")); displaycontent.add(nextofkin); displaycontent.add(new jlabel("relationship next of kin")); displaycontent.add(relationshipwithnextofkin); displaycontent.add(new jlabel("next of kin's phone number(s)")); displaycontent.add(nextofkinphonenumber); displaybutton.add(addnew); displaybutton.add(save); displaybutton.add(view); displaybutton.add(exit); //button actions same other methods. removed because of body character limits } public final void provisionstable() { displayprovisionspanel.setlayout(new borderlayout()); displayprovisionspanel.add(displaycontent, borderlayout.center); displayprovisionspanel.add(displaybutton, borderlayout.south); displayprovisionspanel.add(displaytable, borderlayout.north); displaycontent.add(new jlabel("item name")); displaycontent.add(itemname); displaycontent.add(new jlabel("item price")); displaycontent.add(itemprice); displaycontent.add(new jlabel("item company")); displaycontent.add(itemcompany); displaycontent.add(new jlabel("quantity of items bought")); displaycontent.add(quantityofitemsbought); displaycontent.add(new jlabel("quantity of items sold")); displaycontent.add(quantityofitemssold); displaycontent.add(new jlabel("quantity in stock")); displaycontent.add(quantityinstock); displaybutton.add(addnew); displaybutton.add(save); displaybutton.add(view); displaybutton.add(exit); view.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string db_url = "jdbc:odbc:projectsalesdatabase"; string username = ""; string password = ""; connection con = null; try { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); con = drivermanager.getconnection(db_url, username, password); } catch (classnotfoundexception | sqlexception f) { joptionpane.showmessagedialog(rootpane, f.getmessage()); } statement state = null; resultset set = null; try { string query = "select * provisions"; state = con.createstatement(); set = state.executequery(query); boolean nextrec = set.next(); if (!nextrec) { joptionpane.showmessagedialog(rootpane, "no record"); } else { vector col = new vector(); vector row = new vector(); resultsetmetadata rsm = set.getmetadata(); (int x = 1; x <= rsm.getcolumncount(); x++) { col.addelement(rsm.getcolumnname(x)); } { row.addelement(getnextrow(set, rsm)); } while (set.next()); jtable tab = new jtable(row, col); displaycontent.removeall(); displaytable.removeall(); displaytable.add(new jscrollpane(tab), borderlayout.center); validate(); } state.close(); } catch (sqlexception sql) { joptionpane.showmessagedialog(rootpane, sql.getmessage()); } } }); save.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { try { string query = "insert provisions values ('" + itemname.gettext() + "'," + " '" + itemprice.gettext() + "'," + " '" + itemcompany.gettext() + "'," + " '" + quantityofitemsbought.gettext() + "'," + " '" + quantityofitemssold.gettext() + "'," + " '" + quantityinstock.gettext() + "')"; string db_url = "jdbc:odbc:projectsalesdatabase"; string username = ""; string password = ""; connection con = null; try { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); con = drivermanager.getconnection(db_url, username, password); } catch (classnotfoundexception | sqlexception f) { joptionpane.showmessagedialog(rootpane, f.getmessage()); } statement state = con.createstatement(); int rep = state.executeupdate(query); if (rep == 0) { joptionpane.showmessagedialog(rootpane, "no data saved"); } else { joptionpane.showmessagedialog(rootpane, "data saved"); } } catch (sqlexception sqle) { sqle.getmessage(); } } }); addnew.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { try { itemname.settext(""); itemprice.settext(""); itemcompany.settext(""); quantityofitemsbought.settext(""); quantityofitemssold.settext(""); quantityinstock.settext(""); displaytable.removeall(); displaycontent.removeall(); displaycontent.add(new jlabel("item name")); displaycontent.add(itemname); displaycontent.add(new jlabel("item price")); displaycontent.add(itemprice); displaycontent.add(new jlabel("item company")); displaycontent.add(itemcompany); displaycontent.add(new jlabel("quantity of items bought")); displaycontent.add(quantityofitemsbought); displaycontent.add(new jlabel("quantity of items sold")); displaycontent.add(quantityofitemssold); displaycontent.add(new jlabel("quantity in stock")); displaycontent.add(quantityinstock); validate(); } catch (exception f) { f.getmessage(); } } }); } vector getnextrow(resultset set, resultsetmetadata rsm) { vector currentrow = new vector(); try { (int x = 1; x <= rsm.getcolumncount(); x++) { switch (rsm.getcolumntype(x)) { case types.varchar: currentrow.addelement(set.getstring(x)); break; case types.integer: currentrow.addelement(new long(set.getlong(x))); break; default: system.out.println("no column type known"); break; } } } catch (sqlexception sqle) { sqle.getmessage(); } return currentrow; } }
i don't know why not working correctly. please if can try , compile/run see i'm saying. thanks.
i trying create gui app using jtabbedpane create 5 tabs. each tab contains panel 3 panels in it
as nature of swing components, can embed 1 jcomponent
inside except jframe
.
so, need create jpanel
. make sure has gridlayout
3 cols , 1 row (or other way around, if need).
then, add 3 jpanels
base jpanel has grid layout.
finally, add base jpanel
jtabbedpane
. have @ jpanel
tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
Comments
Post a Comment