简体   繁体   中英

Retrieving array data for JList items

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:

static class Video {
    String vidID;
    String name;
    String writer;

    @Override
    public String toString() {
        return writer; // For JList display
    }
}

The usage then goes as follows.

List<Video> list = VideoData.getVideoList();

JList videolist = new JList();

Vector<Video> data = new Vector<Video>();  
for (int i=0; i < list.size(); i++) {
    Video video = list.get(i);
    data.addElement(video);
}

videolist.setListData(data);
videolist.setSelectedIndex(0);
videolist.addMouseListener(new ActionJList(videolist));
videolist.setFixedCellWidth(300);
add(new JScrollPane(videolist));

with getVideoList as:

List<Video> getVideoList() {
List<Video> list = new ArrayList<Video>();

try {
    ResultSet res = stmt.executeQuery("SELECT VidID, Name, Writer FROM Items ORDER BY VidID ASC");
    while (res.next()) { // there is a result
        Video sub = new Video();
        sub.vidID = res.getString("VidID");
        sub.name = res.getString("Name");
        sub.writer = res.getString("Writer");
        list.add(sub); 
    }
        res.close();
} catch (Exception e) {
    System.out.println(e);
    return null;
}

return list;
}

The JList should be populated with objects that contain all the information needed. Then simply cast the item retrieved (or if using a generic JList, then no need to cast). To get the List to display your items correctly, give it a custom renderer (or a weak way to do this is to give your object a toString() method, but using a renderer is much better). Your list appears to only hold one of the fields returned from the database rather than all the relevant data, and I'd change this.

You can make a hashmap with name as a key and id as value. populate this hashmap while populating JList. in mouseClick event you can get ID using function hashmap.get(name);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM