java - Removing dynamically Objects from JPanel -
via jspinner
want add , remove object "porta" ajpanel
, works fine adding , when remove 1 object if try remove 2 or more objects doesn't work properly. code think issue related last element of vector
on add object "porta":
import javax.swing.jframe; public class preparagara extends jframe { public jspinner spinner; private jpanel panel_1,panel_2; private int spin; private int nporta = 1; private portap scorriporta,porta; final vector<portap> vp = new vector<portap>(); /** * launch application. */ /** * create application. */ public preparagara() { initialize(); } /** * initialize contents of frame. */ private void initialize() { final jframe frame = new jframe(); frame.setvisible(true); frame.setbounds(100, 100, 1000, 400); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(new gridlayout(4, 1, 3, 3)); jmenubar menubar = new jmenubar(); frame.getcontentpane().add(menubar); jmenu mngara = new jmenu("gara"); mngara.setfont(new font("courier new", font.bold, 30)); menubar.add(mngara); panel_1 = new jpanel(); frame.getcontentpane().add(panel_1); panel_1.setlayout(new flowlayout(flowlayout.left, 5, 5)); final jpanel panel_2 = new jpanel(); frame.getcontentpane().add(panel_2); panel_2.setlayout(new flowlayout(flowlayout.left, 5, 5)); jmenuitem mntmporte = new jmenuitem("porte"); mntmporte.setfont(new font("segoe ui", font.plain, 20)); mntmporte.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { jlabel lbl = new jlabel(); lbl.settext("numero porte : "); panel_1.add(lbl); spinnernumbermodel sm =new spinnernumbermodel(0,0,30,1); final jspinner spinner = new jspinner(sm); panel_1.add(spinner); spinner.getfocuslisteners(); spin=0; panel_1.revalidate(); spinner.addchangelistener(new changelistener() { @override public void statechanged(changeevent arg0) { int ora=(int) spinner.getvalue(); if(spin<ora && spin<30){ porta = new portap(nporta); panel_2.add(porta); nporta++; vp.add(porta); panel_2.revalidate(); }else{ //int = vp.size(); panel_2.remove(vp.lastelement()); //panel_2.remove(porta); vp.lastelement().remove(porta); nporta=nporta-1; //system.out.println(i); panel_2.revalidate(); panel_2.repaint(); } spin=ora; }}); } }); mngara.add(mntmporte); jmenuitem mntmgiudici = new jmenuitem("giudici"); mntmgiudici.setfont(new font("segoe ui", font.plain, 20)); mngara.add(mntmgiudici); jmenu mndbase = new jmenu("dbase"); mndbase.setfont(new font("courier new", font.bold, 30)); menubar.add(mndbase); jmenuitem mntmresetpenalit = new jmenuitem("reset penalit\u00e0"); mndbase.add(mntmresetpenalit); jmenuitem mntmresetporte = new jmenuitem("reset porte"); mndbase.add(mntmresetporte); jmenuitem mntmrestgiudici = new jmenuitem("reset giudici"); mntmrestgiudici.setactioncommand("reset giudici"); mndbase.add(mntmrestgiudici); jmenu mnreturn = new jmenu("return"); mnreturn.setfont(new font("courier new", font.bold, 30)); menubar.add(mnreturn); jmenuitem mntmchiudimenu = new jmenuitem("chiudi menu"); mnreturn.add(mntmchiudimenu); jpanel panel = new jpanel(); frame.getcontentpane().add(panel); } }
i've done this. first, recommend portion of panel intend change should separate sub-panel. , when update occurs, call initialize() on sub-panel data indicating should contain. rebuilding panel every time changes. make sure don't have holding onto old components (listeners , such), or else have memory leaks.
someone else may have better solution, cleanest have found works in swing.
Comments
Post a Comment