0

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.

2
  • Are you just trying to get the name of the CD displayed in the List? Commented Mar 7, 2012 at 16:25
  • Essentially, yeah... Just want the elements of discsArray to be the element entries Commented Mar 7, 2012 at 16:50

2 Answers 2

2

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()
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Also consider extending AbstractListModel to leverage existing implementations of some methods.
1

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.

1 Comment

Tried what you said, but we're calling discsArray from another class... but for whatever reason it wont allow me to use the discsArray and asks me to create the method, etc. is there anyway to get around this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.