swing - Java GUI adding a JList to class in another file -
i'm trying call jlist have in 1 class , add no avail, it's telling me static , non static functions
i've got arraylist called finallist in 1 class, filled values, , has been checked printing list out.
then have class in different file called cupboard, want put items jlist there.
finallist.add(si);
is items being added, si array items, , finallist new array in cupboard class file, have
public cupboard() { cupboardcontent = new jlist(shoppinglist.finallist.toarray()); }
where cupboardcontent new jlist want items go.
thanks if has idea. i'm sure it's straightforward, , i'm being quite stupid! it'd seem when combining normal processes gui, i'm fairy new working gui i'm struggling make connections!
//edit
right, first bit of code adding items array absolutely fine, need work out how call in new class. currently, have
public class kitchencupboard extends jpanel //implements actionlistener { private jlist cupboardcontent; private jbutton useditem; shoppinglist items = new shoppinglist(); public kitchencupboard() { system.out.println(shoppinglist.finallist); cupboardcontent = new jlist(items.finallist.toarray()); cupboardcontent.setvisiblerowcount(10); cupboardcontent.setfixedcellheight(30); cupboardcontent.setfixedcellwidth(200); cupboardcontent.setfont(new font ("sansserif", font.bold, 13)); cupboardcontent.setselectionmode(listselectionmodel.multiple_interval_selection); add(new jscrollpane(cupboardcontent)); }
}
the array in shoppinglist is
static arraylist<shoppingitem> finallist = new arraylist<shoppingitem>();
it's using 2 classes/files: shoppinglist.java , kitchencupboard.java
because finallist member of shoppinglist, can 1 of 2 things. either:
declare finallist static
member of shoppinglist, allowing access have above:
cupboardcontent = new jlist(shoppinglist.finallist.toarray());
or pass reference shoppinglist object constructor of cupboard
allowing access finallist through reference:
public cupboard(shoppinglist list) { cupboardcontent = new jlist(list.finallist.toarray()); }
and create new cupboard:
cupboard c = new cupboard(<some shoppinglist instance>);
can suggest cast generated array type need, java 7 introduces new generics based swing components, array of whatever in finallist
more useful.
Comments
Post a Comment