java - how to add items to a list box from another class in netbeans -
i add elements list box on jframe, different class doesn't seem work no matter try... not error or feedback on whats wrong this... heres layout have, listbox using defaultlistmodel lm2
i know how add elements jlist in jframe class, reason unable add elments class when adding inside jframe class:
public void log(string str) { lm2.addelement(str); }
and on "other class"
frmmain doit = new frmmain(); doit.log("add list box");
# more details add- --- >
#i have 3 classes , here are:
frmmain.class
// jframe class builds jframe (from jframe template) public class frmmain extends javax.swing.jframe { /** creates new form frmmain */ public frmmain() { initcomponents(); } public void log(string str) { lm2.addelement(str); } public defaultlistmodel lm2 = new defaultlistmodel(); }
requestinfo.class
// requestinfo.class, trying add item // jlist doesn't add or error public class requestinfo { public void processreturnedinfo(string sdata, boolean bwithlabel) { frmmain fm = new frmmain(); fm.log("test test"); }
rs232example.class
// main class sets jframe visible public class rs232example { public static void main(string[] args) throws exception { frmmain form = new frmmain(); form.setvisible(true); } }
i understand maybe need set form visible on requestinfo.class, can't this, because continuously open form multiple times, because class method called multiple times event...
if listbox-model set right (like listbox.setmodel(this.lm2);
) guess following should work:
public static void main(string[] args) throws exception { frmmain form = new frmmain(); form.setvisible(true); form.log("hallo"); }
if wondering why works , code within requestinfo not, keep in mind, create new frmmain it's own listmodel every call of processreturnedinfo
if want have 1 frame updated try use frame singleton:
change constructor of frmmain
public private , add class:
private static frmmain instance = null; public static frmmain getinstance() { if (instance == null) { instance = new frmmain(); } return instance; }
instead of calling new frmmain()
must use frmmain.getinstance()
in requestinfo
, rs232example
that's how work on same frame.
good luck.
Comments
Post a Comment