java - Adding items to an already existing jlist from another class -
i have jlist (named jlist1) created using desing mode netbeans ide, , want add items list using secondary class parses big xml list , gets data it. problem dont understand how this, tried lot of different codes, tried model too, cant right. new java (and programming too), , dont understand if like
string[] ar = {"one", "two", "three"};
jlist jlist1 = new jlist(ar);
created new jlist instead of using created one, no ?
created using desing mode netbeans ide, maybe not idea prisonier of code generated
add new item defaultlistmodel
and want add items list using secondary class parses big xml list , gets data it.
sounds have issue concurency in swing, updates visible swing gui must done on edt
use
swingworker#publish()long , hard job (which parses big xml list , gets data it.)
for example, add new item defaultlistmodel
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class testing extends jframe { private static final long serialversionuid = 1l; private defaultlistmodel listmodel = new defaultlistmodel(); private jlist list = new jlist(listmodel); private int currentselectedrow = 0; private int xx = 0; public testing() { setlocation(400, 300); setdefaultcloseoperation(exit_on_close); (int x = 0; x < 9; x++) { listmodel.addelement("" + x); xx++; } jscrollpane sp = new jscrollpane(list); add(sp, borderlayout.center); jbutton btn1 = new jbutton("reset model castingmodel"); add(btn1, borderlayout.north); btn1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent ae) { list.clearselection(); defaultlistmodel model = (defaultlistmodel) list.getmodel(); model.removeallelements(); // note swing gui default freeze if removed more // 999 elemets jlist or underlaying xxxlistmodel, // use repeatly actions swingtimer on short period (int x = 0; x < 9; x++) { model.addelement("" + (x + xx)); xx++; } list.setmodel(model); } }); jbutton btn2 = new jbutton("reset model directly model"); add(btn2, borderlayout.south); btn2.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent ae) { list.clearselection(); listmodel.removeallelements(); (int x = 0; x < 9; x++) { listmodel.addelement("" + (x + xx)); xx++; } } }); pack(); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new testing().setvisible(true); } }); } }
Comments
Post a Comment