java - Any way to fix thefollowing warning: javax.swing.JList is a raw type. References to generic type <E> should be parameterized -
i have code shopping cart system , using javax.swing.jlist causing problems. there way fix changing something? should write different way?
here code. (i'm using dr.java, should use other compiler?)
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.io.*; public class shoppingcartsystem extends jframe { private jpanel sourcelistpanel; private jpanel shoppingcartpanel; private jpanel buttonspanel; private jlist sourcelist; private jlist shoppingcart; private jscrollpane scrollpane; private jscrollpane scrollpane2; private jbutton addbutton; private jbutton removebutton; private jbutton checklistbutton; private string []books; private double []price; private string []cart; private int cartsize; private double subtotal = 0.0,tax,total; shoppingcartsystem() { settitle("shopping cart system"); setdefaultcloseoperation(jframe.exit_on_close); setlayout(new borderlayout()); books = new string[25]; price = new double[25]; cart = new string[25]; cartsize =0; readdatafromfile(); buildsourcelistpanel(); buildshoppingcartpanel(); buildbuttonspanel(); add(sourcelistpanel,borderlayout.west); add(shoppingcartpanel,borderlayout.east); add(buttonspanel,borderlayout.south); pack(); setvisible(true); } private void readdatafromfile() { try{ datainputstream dis = new datainputstream(new fileinputstream("bookprices.txt")); bufferedreader br = new bufferedreader(new inputstreamreader(dis)); int i=0; string line; while((line = br.readline()) != null){ string []arr = line.split(", "); books[i] = arr[0]; price[i++] = double.parsedouble(arr[1]); } br.close(); dis.close(); } catch(exception exp) { system.out.println(exp.tostring()); } } private void buildsourcelistpanel() { sourcelistpanel = new jpanel(); sourcelist = new jlist(books); sourcelist.setselectionmode(listselectionmodel.single_selection); sourcelist.setvisiblerowcount(6); scrollpane = new jscrollpane(sourcelist); sourcelistpanel.add(scrollpane); } private void buildshoppingcartpanel() { shoppingcartpanel = new jpanel(); shoppingcart = new jlist(); shoppingcart.setselectionmode(listselectionmodel.single_selection); shoppingcart.setvisiblerowcount(6); scrollpane2 = new jscrollpane(shoppingcart); shoppingcartpanel.add(scrollpane2); } private void buildbuttonspanel() { buttonspanel =new jpanel(); addbutton = new jbutton("add shopping cart"); removebutton = new jbutton("remove shopping cart"); checklistbutton =new jbutton("check list"); buttonspanel.setlayout(new flowlayout(10)); buttonspanel.add(addbutton); buttonspanel.add(removebutton); buttonspanel.add(checklistbutton); addbutton.addactionlistener(new buttonlistener()); removebutton.addactionlistener(new buttonlistener()); checklistbutton.addactionlistener(new buttonlistener()); } private class buttonlistener implements actionlistener { public void actionperformed(actionevent ae) { if(ae.getsource() == addbutton) { cart[cartsize++] = (string) sourcelist.getselectedvalue(); shoppingcart.setlistdata(cart); subtotal += price[sourcelist.getselectedindex()]; } else if(ae.getsource() == removebutton) { for(int i=0;i<books.length;i++) if(shoppingcart.getselectedvalue().equals(books[i])) { subtotal -= price[i]; break; } int selection = shoppingcart.getselectedindex(); for(int i=selection; i< cart.length-1;i++) cart[i] = cart[i+1]; shoppingcart.setlistdata(cart); } else { java.text.decimalformat df = new java.text.decimalformat("#.##"); tax = subtotal * 0.06; total = subtotal + tax; stringbuffer sb = new stringbuffer(); sb.append("sub total: "+df.format(subtotal)+"\n"); sb.append("tax: "+df.format(tax)+"\n"); sb.append("total: "+df.format(total)+"\n"); joptionpane.showmessagedialog(null, sb); } } } public static void main(string []args) { new shoppingcartsystem(); } }
yes. appear want contain string(s), declare jlist string generic type. like
private jlist<string> sourcelist; private jlist<string> shoppingcart; and, initialize them like
sourcelist = new jlist<>(); // ... shoppingcart = new jlist<>(); or, declare , initialize @ same time
private jlist<string> sourcelist = new jlist<>(); private jlist<string> shoppingcart = new jlist<>();
Comments
Post a Comment