swing - Java Combobox with Arraylist error -


i new using swing , using jcombo box, attempting create drop down list train lines, stations, , i'm receiving errors, have feeling may importing data array list wrong, program spits out train line array, console have commented out println command because using gui.

exception in thread "awt-eventqueue-0" java.lang.classcastexception: java.util.arraylist cannot cast javax.swing.comboboxmodel @ touchon.setpanels(touchon.java:63) @ touchon.<init>(touchon.java:52) @ gui$2.actionperformed(gui.java:51) 

i receive above errors below code.

import javax.swing.*;  import java.awt.dialog.modalitytype; import java.awt.event.*; import java.awt.*; import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.io.ioexception; import java.util.arraylist;  public class touchon extends jdialog { private jpanel mainpanel;   public arraylist reader() { try {      arraylist<string> trains = new arraylist<string>();     int count = 0;     string testing = "";     file file = new file("trainlines.txt");     filereader filereader = new filereader(file);     bufferedreader bufferedreader = new bufferedreader(filereader);     stringbuffer stringbuffer = new stringbuffer();     string line;     while ((line = bufferedreader.readline()) != null)      {         stringbuffer.append(line);         count += count;         trains.add(line + "\n");         stringbuffer.append("\n");        }     filereader.close();     //arrays.aslist(trains).stream().foreach(s -> system.out.println(s));     return trains; } catch (ioexception e) {     e.printstacktrace(); } //return tostring(); return null;   }   public touchon() {     setpanels();      setmodalitytype(modalitytype.application_modal);     setsize(200, 200);     setvisible(true); } public void setpanels() {     mainpanel = new jpanel(new gridlayout(0, 2));     jpanel containerpanel = new jpanel(new gridlayout(0, 1));     arraylist stations = reader();     jcombobox<arraylist> cb = new jcombobox<arraylist>((comboboxmodel<arraylist>) stations);     jpanel lowerpanel = new jpanel(new flowlayout());     jbutton apply = new jbutton("touch on ?");     jbutton cancel = new jbutton("cancel");     cancel.addactionlistener(new actionlistener(){         public void actionperformed(actionevent e){             dispose();         }     });     apply.addactionlistener(new actionlistener()     {         public void actionperformed(actionevent e)         {             system.out.println("hello");         }     });      jlabel touchondate = new jlabel("date: ");     jlabel touchontimehr = new jlabel("time hour: ");     jlabel touchontimem = new jlabel("time minute:");     jlabel station = new jlabel("station: ");      jtextfield touchonfielddate = new jtextfield();     jtextfield touchontimefieldhour = new jtextfield();     jtextfield touchontimefieldminute = new jtextfield();     //jtextfield touchonstation = new jtextfield();       cb.setvisible(true);      mainpanel.add(touchondate);     mainpanel.add(touchonfielddate);     mainpanel.add(touchontimehr);     mainpanel.add(touchontimefieldhour);     mainpanel.add(touchontimem);     mainpanel.add(touchontimefieldminute);     mainpanel.add(station);     mainpanel.add(cb);     //mainpanel.add(touchonstation);     lowerpanel.add(apply);     lowerpanel.add(cancel);     touchontimefieldhour.setsize(10,10);     containerpanel.add(mainpanel);     containerpanel.add(lowerpanel);      add(containerpanel); }  } 

be kind new , articles or tutorials read appreciated, learning java difficult.

shouldn't (comboboxmodel<arraylist>) stations stations? there doesn't seem need cast

mind you...

arraylist stations = reader(); jcombobox<arraylist> cb = new jcombobox<arraylist>((comboboxmodel<arraylist>) stations); 

should more like...

arraylist<string> stations = reader(); jcombobox<string> cb = new jcombobox<>(); (string value : stations) {     cb.additem(value); } 

or

arraylist<string> stations = reader(); jcombobox<string> cb = new jcombobox<>(stations.toarray(new string[stations.size()])); 

if you're feeling lazy...

take closer @ how use combo boxes more details


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -