Thursday, 15 August 2013

swing - (Java) JPanel Not Printing Out -



swing - (Java) JPanel Not Printing Out -

i'm doing jpanel assignment in college course of study , i'm having problem final print out. panel looks great , everything, thing can't display line supposed printed out.

it's designed take input of grocery item, name, price, category , quantity beingness input user. when "add cart" button clicked, should display info typed in. of right not.

here's 2 classes i'm using:

mycart.java

package assignment2; import javax.swing.*; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; public class mycart extends jframe { private jpanel display; private jlabel itemlabel; private jtextfield item; private jlabel catlabel; private jcombobox category; private jlabel quanlabel; private jtextfield quantity; private jlabel pricelabel; private jtextfield price; private jpanel btns; private jbutton addbtn; private jbutton exitbtn; private jpanel list; private jlist mylist; private arraylist<item> itemlist; public mycart() { itemlist = new arraylist(); mylist = new jlist(itemlist.toarray()); this.setlayout(new gridlayout(2,1,5,5)); display = new jpanel(); display.setlayout(new gridlayout(5,2,5,5)); itemlabel = new jlabel(" item: "); item = new jtextfield(10); catlabel = new jlabel(" category: "); category = new jcombobox(new string[]{"meat", "fruit/vegetable", "dairy", "grains", "sweets", "other"}); quanlabel = new jlabel(" quantity: "); quantity = new jtextfield(10); pricelabel = new jlabel(" price: "); cost = new jtextfield(10); display.add(itemlabel); display.add(item); display.add(catlabel); display.add(category); display.add(quanlabel); display.add(quantity); display.add(pricelabel); display.add(price); this.add(display); btns = new jpanel(); btns.setlayout(new gridlayout(1,3,5,5)); addbtn = new jbutton("add cart"); exitbtn = new jbutton("exit"); btns.add(addbtn); btns.add(exitbtn); this.add(btns); list = new jpanel(); list.setlayout(new flowlayout()); list.add(new jlabel("my cart:")); jscrollpane myscrollpane = new jscrollpane(mylist); list.add(myscrollpane); setlayout(new borderlayout()); this.add(display,borderlayout.north); this.add(btns,borderlayout.center); this.add(list,borderlayout.south); //event listener object created buttonlisteners buttonlistener = new buttonlisteners(); addbtn.addactionlistener(buttonlistener); exitbtn.addactionlistener(buttonlistener); } public static void main(string[] args) { mycart cart = new mycart(); cart.settitle("cart <adam>"); cart.setsize(400,350); cart.setlocationrelativeto(null); cart.setvisible(true); cart.setdefaultcloseoperation(jframe.exit_on_close); } //event listener class adding class buttonlisteners implements actionlistener { @override public void actionperformed(actionevent e) { switch(e.getactioncommand()) { case "add": string n = item.gettext(); string c = category.getselecteditem().tostring(); double q = double.valueof(quantity.gettext()); double p = double.valueof(price.gettext()); item myitem = new item(); myitem.setname(n); myitem.setcategory(c); myitem.setquantity(q); myitem.setprice(p); itemlist.add(myitem); mylist.setlistdata(itemlist.toarray()); break; case "exit": system.exit(0); } } } //exit listener class exitlistener implements actionlistener{ @override public void actionperformed(actionevent e){ system.exit(0); } } }

item.java

package assignment2; public class item { private string name; private string category; private double price; private double quantity; public item(string name, string category, double price, double quantity) { } public item() { } public string getname() { homecoming name; } public void setname(string name) { this.name = name; } public string getcategory() { homecoming category; } public void setcategory(string category) { this.category = category; } public double getprice() { homecoming price; } public void setprice(double price) { this.price = price; } public double getquantity() { homecoming quantity; } public void setquantity(double quantity) { this.quantity = quantity; } public double calcamount() { homecoming quantity * price; } public string tostring(){ return(name + ", " + category + ", " + quantity + ", " + cost + ", " + calcamount()); } }

you forgot add together actioncommand jbutton checks case statement in actionlistener

everytime phone call switch(e.getactioncommand()) listener grabbing actioncommand of button added, if not added name of button actioncommand.

solution:

addbtn = new jbutton("add cart"); addbtn.setactioncommand("add"); //will phone call add together case actionlistener

java swing jpanel

No comments:

Post a Comment