In the .NET world there's quite often discussion on the most what the appropriate container is for a set of objects. That's probably because we've got so many to choose from: Stack, ArrayList, ListDictionary, HashTables, and so on. I'm sure there's just as many in Java to choose from, but since I'm not a Java guy like David Shay (who discovered today's example), I really couldn't enumerate them.
But no less, I'm still pretty sure that using a JList (like a select/dropdown box) is not the appropriate place to store objects for a middle tier (non-UI) component ... and even if it is ... I'm not sure that's the right way to do it, either ...
JList atmTrafficDescrsList = new JList(DataStore.getObjectList("ATMTrafficDescr").toArray());
int listLength = atmTrafficDescrsList.getModel().getSize(); ATMTrafficDescr[] descrs = new ATMTrafficDescr[listLength]; int enabledDescrs = 0;
for(int i = 0; i < listLength; i++){ descrs[i] = ((ATMTrafficDescr)DataStore.getObject(atmTrafficDescrsList.getModel().getElementAt(i).toString())); if(descrs[i].getEnabled() && (descrs[i].getServiceClass().compareTo("CBR") == 0)){ enabledDescrs++; } }
String[][] data = new String[enabledDescrs][2];
int j = 0; // only enabled descrs will be in table for(int i = 0; i < listLength; i++){ if(descrs[i].getEnabled() && (descrs[i].getServiceClass().compareTo("CBR") == 0)){ data[j][0] = descrs[i].getName(); data[j][1] = descrs[i].getPcr(); j++; } }
David explains a bit more ...
The DataStore.getObjectList() method is returning a List. But since you can not put a List in a JList, you have to convert it to an array using the toArray() method. The loop is putting the objects into a new array, but instead of simply copying the object reference, in gets their name using the toString() method, and retrieves the object reference again from the DataStore using the getObject() method.