Reputation:
Basically, I've got a CD Library that holds instances of a CD ArrayList<>, I've then got a people ArrayList<> that can "borrow" CD's... from that CD's are added to an available, or unavailable list.
public CDStore(String storeNme) {
storeName = storeNme;
discsArray = new ArrayList<CD>();
peopleArray = new ArrayList<Person>();
}
By using a JList, I'm trying to make the elements of the list equal the instances of CD.
So... item1 in the list would be the CD at index 0, item 2 = index 1 and so on....
String[] entries = ??????????????????;
JList listOfCD = new JList(entries);
listOfCD.setVisibleRowCount(4);
JScrollPane listPane = new JScrollPane(listOfCD);
JTextField valueField = new JTextField("None", 7);
Thankyou.
Upvotes: 0
Views: 4814
Reputation: 2006
Your CDStore
could implement the interface ListModel
. Than you can use the CDStore
as a model for your JList
.
CDStore store = new CDStore("Store");
// add some CDs
JList listOfCD = new JList(store);
listOfCD.setVisibleRowCount(4);
JScrollPane listPane = new JScrollPane(listOfCD);
JTextField valueField = new JTextField("None", 7);
Here is example implementation of CDStore implements ListModel
. Everytime the discsArray
changes you should call the method fireContentsChanged
.
public class CDStore implements ListModel {
private String storeName;
private List<CD> discsArray;
private List<Person> peopleArray;
public CDStore(String storeNme) {
storeName = storeNme;
discsArray = new ArrayList<CD>();
peopleArray = new ArrayList<Person>();
}
//your methods
//ListModel
private List<ListDataListener> listener = new ArrayList<ListDataListener>();
public void addListDataListener(ListDataListener l) {
listener.add(l);
}
public void removeListDataListener(ListDataListener l) {
listener.remove(l);
}
protected void fireContentsChanged() {
for (ListDataListener l : listener) {
l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, discsArray.size()-1));
}
}
public Object getElementAt(int index) {
return discsArray.get(index);
}
public int getSize() {
return discsArray.size()
}
}
Upvotes: 2
Reputation: 411
If you have a method to retrieve CD name, let's call it getCDName(), you can try
String[] entries = new String[discsArray.length];
int index = 0;
for (CD cd: discsArray) {
entries[index++] = cd.getCDName();
}
This should fill in your entries array.
Upvotes: 1