Sunday, 15 February 2015

arraylist - Java with JPanel -



arraylist - Java with JPanel -

i'm building form in user can come in in employee info , display in textarea field have display or "see all" button cant work. \

i saving each submission saved array called resultshold , array saved array called line both arrays hold 1 submission instead of multiple - tested , each time display contents of array 'line' shows lastly submission meaning not storing mutiple submission each time calculate button clicked - ive added counter line array set position of store "text" variable saves 1 , not mutiple- can help or guide me in right direction - below code.

public class container extends jframe { arraylist<string> resultshold = new arraylist<>(); arraylist line = new arraylist<>(); //private string[] resultshold; private final jlabel employeenamelabel; private final jlabel hoursworkedlabel; private final jlabel department; private final jlabel emptycell; private final jlabel wagelabel; private final jlabel departmentresultslabel; private final jtextfield employeenametexfield; private final jtextfield hoursworkedtextfield; private final jtextfield wagetextfield; private final jtextarea resultsscreen; public jbutton seeall; public jbutton myexitbutton; public jbutton calcbutton; public jbutton clearbutton; jpanel centerpanel; jpanel buttonpanel; jpanel textfieldpanel; jpanel myresultspanel; public container() { super("payroll program"); string[] departments = {"choice 1", "choice 2", "choice 3", "choice 4", "choice 5", "choice 6"}; final jcombobox<string> departmentsv = new jcombobox<string>(departments); departmentsv.setvisible(true); getcontentpane().setlayout(new gridlayout(4, 5, 8, 8)); //creating text fields, labels , buttons emptycell = new jlabel(""); departmentresultslabel = new jlabel(); employeenamelabel = new jlabel("first name"); hoursworkedlabel = new jlabel("hours worked"); wagelabel = new jlabel("wage"); section = new jlabel("department"); employeenametexfield = new jtextfield(5); hoursworkedtextfield = new jtextfield(5); wagetextfield = new jtextfield(5); resultsscreen = new jtextarea(20, 50); myexitbutton = new jbutton("exit"); myexitbutton.setsize(new dimension(10, 10)); calcbutton = new jbutton("calc"); calcbutton.setsize(new dimension(10, 10)); clearbutton = new jbutton("clear"); clearbutton.setsize(new dimension(10, 10)); seeall = new jbutton("see results"); seeall.setsize(new dimension(10, 10)); //adding labels centerpanel = new jpanel(); buttonpanel = new jpanel(); textfieldpanel = new jpanel(); myresultspanel = new jpanel(); centerpanel.setlayout(new gridlayout(0, 4, 8, 8)); centerpanel.add(employeenamelabel); centerpanel.add(hoursworkedlabel); centerpanel.add(wagelabel); centerpanel.add(department); // adding text fields textfieldpanel.setlayout(new gridlayout(0, 4, 8, 8)); textfieldpanel.add(employeenametexfield); textfieldpanel.add(hoursworkedtextfield); textfieldpanel.add(wagetextfield); textfieldpanel.add(departmentsv); //adding buttons panel buttonpanel.setlayout(new gridlayout(0, 4, 8, 8)); buttonpanel.add(seeall); buttonpanel.add(myexitbutton); buttonpanel.add(calcbutton); buttonpanel.add(clearbutton); myresultspanel.setlayout(new gridlayout(0, 1, 8, 8)); myresultspanel.add(emptycell); myresultspanel.add(resultsscreen); myresultspanel.add(emptycell); myresultspanel.add(emptycell); getcontentpane().add(centerpanel, borderlayout.north); getcontentpane().add(textfieldpanel, borderlayout.north); getcontentpane().add(buttonpanel, borderlayout.north); getcontentpane().add(myresultspanel, borderlayout.north); //clear button action event listener clearbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { employeenametexfield.settext(""); wagetextfield.settext(""); hoursworkedtextfield.settext(""); resultsscreen.settext(""); } }); //end clear listener calcbutton.addactionlistener(new actionlistener() { private int counter; @override public void actionperformed(actionevent e) { string mymy[]; counter++; string hoursc; string name; double total; string wagec; name = employeenametexfield.gettext(); wagec = wagetextfield.gettext(); hoursc = hoursworkedtextfield.gettext(); total = double.parsedouble(hoursc) * double.parsedouble(wagec); departmentresultslabel.settext((string) departmentsv.getselecteditem()); string text = name + "'s" + " weekly pay is: $" + total + "..." + "department: " + departmentsv.getselecteditem(); // resultsscreen.append( // name +"'s"+" weekly pay is: $" + total + "..." + "department: "+departmentsv.getselecteditem()); resultshold = new arraylist<>(); resultshold.add(text); //resultshold.set(counter,text); // line= new arraylist<>(); line.add(counter, resultshold); // system.out.println(line.size()); // if(resultshold.size()>0){ system.out.println(resultshold.size()); resultsscreen.append(resultshold.tostring()); // } system.out.println(counter); employeenametexfield.settext(""); wagetextfield.settext(""); hoursworkedtextfield.settext(""); resultsscreen.settext(""); //resultshold.add(name); //resultshold.add(wagec); // resultshold.add(hoursc); // resultshold.add(total.tostring()); // resultshold.add((string) departmentsv.getselecteditem()); //line.set(i, resultshold.get(i)); // line.add(resultshold); // system.out.println(line); resultsscreen.append(line.tostring()); } }); //event listeners myexitbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { //exit programme system.exit(0); } }); seeall.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { (int = 0; < line.size(); i++) { resultsscreen.append(line.get(i).tostring()); system.out.println(line.get(i)); //system.out.println(line.size()); } } }); } public static void main(string[] args) { container guiwindow = new container(); guiwindow.showwindow(); } public void showwindow() { this.setdefaultcloseoperation(exit_on_close); this.setbounds(100, 100, 400, 300); this.setvisible(true); } }

resultshold = new arraylist<>(); resultshold.add(text);

so, every time button clicked, you're recreating new list , store text inside. don't want create new list. want add together existing list.

note code awful: public fields, utilize of raw types, code commented out, java conventions not respected @ all, meaningless variable names...

java arraylist jpanel

No comments:

Post a Comment