I have an (access) database table which contains data I would like to populate into a list in my java program. The table consists of multiple columms (id, name, etc)
What I would like is a JList listing all the name(s) and then when an item from the list has been double clicked I would like the id number for that item to be inserted into another table.
I have already implemented the list and populated it with the records from the db table (name column). What I’m having trouble with is when the user double-clicks on the item, how do I can I get get the ID for that video? The database call which populates the list selects multiple columns and puts them all into an array, not quite sure how I can link that list item to the array.
Here is what I got so far…
The java class
ArrayList list = new ArrayList();
ArrayList video = new ArrayList();
list = VideoData.getVideoList();
JList videolist = new JList();;
Vector data = new Vector();;
for (int i=0; i < list.size(); i++) {
video = (ArrayList) list.get(i);
data.addElement(video.get(3));
}
videolist.setListData(data);
videolist.setSelectedIndex(0);
videolist.addMouseListener(new ActionJList(videolist));
videolist.setFixedCellWidth(300);
add(new JScrollPane(videolist));
What getVideoList() contains
ArrayList list = new ArrayList();
try {
ResultSet res = stmt.executeQuery("SELECT * FROM Items ORDER BY VidID ASC");
while (res.next()) { // there is a result
ArrayList sub = new ArrayList();
sub.add(res.getString("VidID"));;
sub.add(res.getString("Name"));
sub.add(res.getString("Writer"));
// add sub array to list
list.add(sub);
}
} catch (Exception e) {
System.out.println(e);
return null;
}
return list;
The current doulbe-click function is the following (which I found on the net)
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 2){ // double click
int index = list.locationToIndex(e.getPoint());
ListModel dlm = list.getModel();
Object item = dlm.getElementAt(index);;
list.ensureIndexIsVisible(index);
System.out.println("Double clicked on " + item);
}
}
.. From this item only tells me what is listed inside the JList cell, What I need to to be able to do is get the other array data for that selected item.
(I have used generics
<...>to make the code more readable.)As @HovercraftFullOfEels said, the JList may contain a entire video object:
The usage then goes as follows.
with getVideoList as: