I'm trying to create a JList that displays items of an object that I created.
The object is an items
object and the items
class looks like this:
public class items {
private ArrayList<Item> itemlistweapons = new ArrayList<Item>();
private ArrayList<Item> itemlistapparel = new ArrayList<Item>();
private ArrayList<Item> itemlistaid = new ArrayList<Item>();
private ArrayList<Item> itemlistmisc = new ArrayList<Item>();
public void additem(String name, String type){
if("Weapon".equals(type)){
itemlistweapons.add(new Item(name, type));
}
else if ("Apparel".equals(type)){
itemlistapparel.add(new Item(name, type));
}
else if ("Aid".equals(type)){
itemlistaid.add(new Item(name, type));
}
else if ("Misc.".equals(type)){
itemlistmisc.add(new Item(name, type));
}
}
public void dropitem(String name){
for(int i = 0; i < itemlistweapons.size(); i++){
if(itemlistweapons.get(i).getname().equals(name)){
itemlistweapons.remove(i);
}
}
for(int i = 0; i < itemlistapparel.size(); i++){
if(itemlistapparel.get(i).getname().equals(name)){
itemlistapparel.remove(i);
}
}
for(int i = 0; i < itemlistaid.size(); i++){
if(itemlistaid.get(i).getname().equals(name)){
itemlistaid.remove(i);
}
}
for(int i = 0; i < itemlistmisc.size(); i++){
if(itemlistmisc.get(i).getname().equals(name)){
itemlistmisc.remove(i);
}
}
}
public ArrayList<Item> getItemlistweapons() {
return itemlistweapons;
}
public ArrayList<Item> getItemlistapparel() {
return itemlistapparel;
}
public ArrayList<Item> getItemlistaid() {
return itemlistaid;
}
public ArrayList<Item> getItemlistmisc() {
return itemlistmisc;
}
@Override
public String toString(){
String items = "";
if (itemlistweapons.size() > 0){
items += "Weapons\n";
for (Item itemlist1 : itemlistweapons) {
items += itemlist1 + "\n";
}
}
if (itemlistapparel.size() > 0){
items += "Apparel\n";
for (Item itemlist2 : itemlistapparel) {
items += itemlist2 + "\n";
}
}
if (itemlistaid.size() > 0){
items += "Aid\n";
for (Item itemlist3 : itemlistaid) {
items += itemlist3 + "\n";
}
}
if (itemlistmisc.size() > 0){
items += "Misc.\n";
for (Item itemlist4 : itemlistmisc) {
items += itemlist4 + "\n";
}
}
return items;
}
}
So, how would I take the variable itemlistweapons
and set it so that it displays on a JList. The itemlistweapons
is an array list of the Item
time, and the Item
class looks like this:
public class Item {
private String name;
private String type;
public Item(String itemname, String itemtype){
name = itemname;
type = itemtype;
}
public String getname(){
return name;
}
@Override
public String toString(){
String iteminfo;
iteminfo = name;
return iteminfo;
}
}
Can someone please tell me how to take an ArrayList<Item>
from an items
object and put it into a JList?
I think you may have a 3rd class with the GUI, in there you have the Jlist object
then you can do:
JList list = new JList(itemlistweapons .toArray());
and repeat respectively to any arrayList you want to display there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.