java - Refreshing JList in GUI on button event -
i've looked online how update jlist
after adding model. automatically, others have manually it. i've tried both, , haven't had success. i'm trying update model addelement
method, , goes through. i've walked through code , passed correctly. gui doesn't refresh.
package com.user.tutorial; import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jpanel; import java.awt.borderlayout; import javax.swing.jbutton; import java.awt.event.actionlistener; import java.awt.event.actionevent; import javax.swing.defaultlistmodel; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.box; import javax.swing.jmenu; import javax.swing.jtextpane; import javax.swing.listselectionmodel; import java.awt.component; import java.awt.color; import java.awt.dimension; import java.awt.font; import java.awt.gridlayout; import javax.swing.jlabel; import javax.swing.boxlayout; import javax.swing.jlist; import javax.swing.abstractlistmodel; import java.awt.gridbaglayout; import java.awt.gridbagconstraints; import java.awt.systemcolor; import java.awt.insets; import java.util.list; public class window { private jframe frame; public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { window window = new window(); window.frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } public window() { initialize(); } /** * initialize contents of frame. window builder * generate code. */ public void addto(jpanel displaypanel, component contenttoadd) { displaypanel.add(contenttoadd); } public void initialize() { dimension sidepanelsize = new dimension(180, 540); frame = new jframe(); frame.setbounds(100, 100, 480, 540); frame.setdefaultcloseoperation(jframe.exit_on_close); jmenubar menubar = new jmenubar(); frame.setjmenubar(menubar); jmenu mnfile = new jmenu("file"); menubar.add(mnfile); jmenuitem mntmload = new jmenuitem("load"); mnfile.add(mntmload); jmenuitem mntmsave = new jmenuitem("save"); mnfile.add(mntmsave); jmenuitem mntmexit = new jmenuitem("exit"); mnfile.add(mntmexit); final jpanel itempanel = new jpanel(); itempanel.setpreferredsize(sidepanelsize); itempanel.setbackground(color.white); itempanel.setlayout(new borderlayout()); defaultlistmodel<string> model = new defaultlistmodel<>(); (string items : new string [] {"case", "motherboard", "cpu", "gpu", "psu", "ram", "hdd"}) model.addelement(items); final jlist<string> itemlist = new jlist<>(); itemlist.setmodel(model); itemlist.setselectionmode(listselectionmodel.single_selection); itemlist.setfont(new font("segoeui", font.bold, 11)); itempanel.add(itemlist, borderlayout.west); frame.getcontentpane().add(itempanel, borderlayout.west); final jlist<string> addtolist = new jlist<>(new defaultlistmodel<string>()); jpanel buttoncontainer = new jpanel(); jpanel buttonlist = new jpanel(); gridbagconstraints c = new gridbagconstraints(); buttonlist.setlayout(new boxlayout(buttonlist, boxlayout.y_axis)); buttoncontainer.setlayout(new gridbaglayout()); buttoncontainer.setbackground(new color(238, 238, 238)); final jpanel displaypanel = new jpanel(); displaypanel.setpreferredsize(sidepanelsize); displaypanel.setbackground(color.white); frame.getcontentpane().add(displaypanel, borderlayout.east); // right here button action. `displaymodel` model of `jlist` i'm updating. jbutton addbutton = new jbutton(">>"); addbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { list<string> selected = itemlist.getselectedvalueslist(); defaultlistmodel<string> displaymodel = (defaultlistmodel<string>) addtolist.getmodel(); (string item: selected) { displaymodel.addelement(item); } } }); buttonlist.add(addbutton); jbutton deletebutton = new jbutton("<<"); deletebutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { } }); buttonlist.add(deletebutton); c.gridx = 0; c.gridy = 0; buttoncontainer.add(buttonlist, c); frame.getcontentpane().add(buttoncontainer, borderlayout.center); } }
i'm not sure going wrong. i've checked documentation. guess i'm missing simple. help.
everything seems passed there isn't change gui.
did read point "3" of suggestion of possible problems?
Comments
Post a Comment