java - My new dataModel doesn't replace the old one in my JList [UPDATED] -
i'm working in little program allows place things on map, give name or description. places can have category choose jlist defaultlistmodel. if have created categories , choose save project , continue other time, when load saved file loads perfect except categorylist. it's still blank, , if have project categories , load new project old categories still there.. can't figure out whats wrong, i've must have missed update here?
package test; import javax.swing.*; import javax.swing.event.listselectionevent; import javax.swing.event.listselectionlistener; import javax.swing.filechooser.filenameextensionfilter; import java.awt.*; import java.util.list; import java.awt.event.*; import java.io.*; import java.io.file; import java.util.*; public class testar extends jframe { jcombobox place; jtextfield searchfield; jbutton searchbutton, hidebutton, deleteplacebutton, whatishere, hidecat, newcat, delcat; jfilechooser jfc = new jfilechooser("."); boolean changed = false; defaultlistmodel<placecategory> datamodel = new defaultlistmodel<>(); jlist<placecategory> categorylist = new jlist<placecategory>(datamodel); // defaultlistmodel<placecategory> newdatamodel = new defaultlistmodel<>(); testar(){ super("test"); //filemenu topp jmenubar filebar = new jmenubar(); setjmenubar(filebar); jmenu archive = new jmenu("file"); filebar.add(archive); jmenuitem open = new jmenuitem("open"); archive.add(open); open.addactionlistener(new openlis()); jmenuitem save = new jmenuitem("save"); archive.add(save); save.addactionlistener(new savelis()); //kategorier Öst jpanel east = new jpanel(); add(east, borderlayout.center); east.add(new jlabel("categories")); categorylist.setvisiblerowcount(3); categorylist.setfixedcellwidth(50); east.add(new jscrollpane(categorylist)); categorylist.setselectionmode(listselectionmodel.single_selection); //actionlistener hidecat = new jbutton("hide category"); east.add(hidecat); newcat = new jbutton("new category"); east.add(newcat); newcat.addactionlistener(new newcatlis()); delcat = new jbutton("delete category"); east.add(delcat); boxlayout eastlayout = new boxlayout(east, boxlayout.y_axis); east.setlayout(eastlayout); setdefaultcloseoperation(exit_on_close); setsize(300,300); setvisible(true); setlocationrelativeto(null); setresizable(false); } class newcatlis implements actionlistener{ public void actionperformed(actionevent ave){ string categoryname; color color = color.black; categoryname = joptionpane.showinputdialog(testar.this, "name on category"); color = jcolorchooser.showdialog(testar.this,"chooser color", color); placecategory pc = new placecategory(categoryname, color); datamodel.addelement(pc); } } class openlis implements actionlistener{ public void actionperformed(actionevent ave){ int answer = jfc.showsavedialog(testar.this); if(answer != jfilechooser.approve_option){ return; } file file = jfc.getselectedfile(); string filename = file.getabsolutepath(); try{ fileinputstream fis = new fileinputstream(filename); objectinputstream ois = new objectinputstream(fis); datamodel = (defaultlistmodel)ois.readobject(); system.out.println(datamodel); // newdatamodel = datamodel; // // datamodel.clear(); // // for(int i=0; < newdatamodel.size(); i++){ // placecategory pc = newdatamodel.get(i); // datamodel.addelement(pc); // system.out.println("addar"); // } // // newdatamodel.clear(); ois.close(); fis.close(); pack(); validate(); repaint(); changed = false; } catch(classnotfoundexception e){ joptionpane.showmessagedialog(testar.this, "something went wrong... "+e.getmessage()); } catch(filenotfoundexception e){ joptionpane.showmessagedialog(testar.this, "can not open file..."); } catch(ioexception e){ joptionpane.showmessagedialog(testar.this, "something went wrong... "+e.getmessage()); } } } class savelis implements actionlistener{ public void actionperformed(actionevent ave){ int answer = jfc.showsavedialog(testar.this); if(answer != jfilechooser.approve_option){ return; } file file = jfc.getselectedfile(); string filename = file.getabsolutepath(); try{ fileoutputstream fos = new fileoutputstream(filename); objectoutputstream oos = new objectoutputstream(fos); oos.writeobject(datamodel); oos.close(); fos.close(); changed = false; } catch(filenotfoundexception e){ joptionpane.showmessagedialog(testar.this, "can not open file..."); } catch(ioexception e){ joptionpane.showmessagedialog(testar.this, "something went wrong... "+e.getmessage()); } } } public static void main(string[] args){ new testar(); } }
and placecategory ofc!
package test; import java.awt.*; import java.io.*; public class placecategory implements serializable { private string name; public color color; public placecategory(string name, color color){ this.name = name; this.color = color; } public string tostring(){ return name; } public color getcolor() { return color; } public string getname(){ return name; } }
found way make happen! added after load:
categorylist.setmodel(datamodel);
Comments
Post a Comment