java - Populating JList from inside actionlistener with arraylist -
i'm trying populate jlist
anarraylist<member>
inside actionlistener
method.
public class guidemo extends jframe implements listselectionlistener { ... private jlist list = new jlist(); private jscrollpane sp = new jscrollpane(); ... actionlistener list10 = new actionlistener() { public void actionperformed(actionevent e) { win5.setvisible(true); p1.setvisible(false); string familyname = null; int id = 0; try { familyname = (txt4.gettext()); id = integer.parseint(txt3.gettext()); } catch (numberformatexception ne) { system.out.println("error"); id = 0; } try{ arraylist<member> result = getmembersforlist(id, familyname); list = new jlist(result.toarray()); sp = new jscrollpane(list); } catch (sqlexception se) { txt.settext("sqlexception!"); } catch ( classnotfoundexception ce) { txt.settext("classnotfoundexception!"); } } };
result arraylist<member>
i'm trying populate jlist
with, create interface user can choose between objects in arraylist
.
furthermore:
... b14.addactionlistener(list10); ... win5.setpreferredsize(new dimension(400, 400)); win5.setlayout(new gridlayout(10,1)); win5.setminimumsize(new dimension(400, 400)); win5.add(b13);win5.add(l9);win5.add(txt3);win5.add(l10); win5.add(txt4);win5.add(b14);win5.add(l11); win5.add(sp); list.setvisiblerowcount(15); list.setselectionmode(listselectionmodel.single_selection); list.addlistselectionlistener(this); ...
as can see jlist
located below 2 textfields(txtx)
,labels(lx)
, buttons (bx)
.
public void valuechanged(listselectionevent e){ }
i believe problem stems fact declare jlist
, jscrollpane
outside actionlistener
, cannot populate jlist
. valuechanged
method not done (i'd have solve problem first) don't believe problem stems fact empty.
populating jlist
outside actionlistener
not work since arraylist
has yet filled. idea search either id or familyname or both , number of choices based on matches sqlite db. contents of result arraylist
objects based on matches db.
i believe real rookie mistake, appreciate can get.
the message get:
first, -xlint
flag providing warnings lack of generics.
this means compiler can not guarantee data jlist
expecting data providing. is, way have set up, jlist
expecting elements of type object, providing memeber
.
this not issue, long you've ensured renderers can handle this. it's compiler telling you, has no way sure correct behaviour or not...this might cause issues @ run time.
it preferred possible, tp provide support generics, it's not possible.
private jlist<member> list ...; //... arraylist<member> result = getmembersforlist(id, familyname); list = new jlist(result.toarray(new member[result.size()]));
this allow trap possible errors @ compile time rather hitting error @ run time...
second, let me make guesses...
you create instance of jlist
, jscrollpane
...
private jlist list = new jlist(); private jscrollpane sp = new jscrollpane();
you add these ui...
sp.setviewportview(list); add(sp); // example...
then in actionperformed
method, create new instances of these objects...
list = new jlist(result.toarray()); sp = new jscrollpane(list);
but never add them ui , how magically expect know how link previous instances , update ui...?
that's not how works. instances have created in actionperformed
method have no relationship objects created previously. instead, should changing jlist
's model, example...
actionlistener list10 = new actionlistener() { public void actionperformed(actionevent e) { //... try{ //arraylist<member> result = getmembersforlist(id, familyname); //list = new jlist(result.toarray()); //sp = new jscrollpane(list); defaultlistmodel model = new defaultlistmodel(); (member member : getmembersforlist(id, familyname)) { model.addelement(member); } list.setmodel(model); //... } };
Comments
Post a Comment