2

我有一个 MySQL 数据库,其中包含我想在我的 java 程序中填充到 JList 中的数据。我有两个JList,一个是填写事件标题,第二个是填写来宾姓名。

我想要的是当用户单击任何事件标题时,第二个 JList 将显示属于该事件的所有来宾名称。

我已经成功地用所有事件标题填充了第一个 JList。我遇到的问题是当用户单击事件标题时,客人姓名将在第二个 JList 上显示两次。我怎样才能让它只显示一次?

这是我到目前为止所得到的...

Java 类

private JList getJListEvents() {
    if (jListEvents == null) {
        jListEvents = new JList();
        Border border = BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(1, Color.black, Color.black), "Events", TitledBorder.LEFT, TitledBorder.TOP);
        jListEvents.setBorder(border);
        jListEvents.setModel(new DefaultListModel());
        jListEvents.setBounds(new Rectangle(15, 60, 361, 421));

        Events lEvents = new Events();
        lEvents.loadEvents(jListEvents);

        jListEvents.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                EventC eventC = new EventC();
                //eventC.MonitorRegDetailsInfo(jListEvents, jTextFieldEventName, jTextFieldEventVenue, jTextFieldEventDate, jTextFieldEventTime, jTextAreaEventDesc);
                //eventC.MonitorRegPackageInfo(jListEvents, jTextFieldBallroom, jTextFieldBallroomPrice, jTextFieldMeal, jTextFieldMealPrice, jTextFieldEntertainment, jTextFieldEntertainmentPrice);
                eventC.MonitorRegGuest(jListEvents, jListGuest);
            }
        });
    }
    return jListEvents;
}

控制器类

public void MonitorRegGuest(JList l, JList l2){
    String event = l.getSelectedValue().toString();
    Events retrieveGuest = new Events(event);
    retrieveGuest.loadGuests(l2);
}

带有所有 sql 语句的类

public void loadGuests(JList l){
    ResultSet rs = null;
    ResultSet rs2 = null;
    ResultSet rs3 = null;
    MySQLController db = new MySQLController();
    db.getConnection();

    String sqlQuery = "SELECT MemberID FROM event WHERE EventName = '" + EventName + "'";
    try {
        rs = db.readRequest(sqlQuery);
        while(rs.next()){
            MemberID = rs.getString("MemberID");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    String sqlQuery2 = "SELECT GuestListID FROM guestlist WHERE MemberID = '" + MemberID + "'";
    try {
        rs2 = db.readRequest(sqlQuery2);
        while(rs2.next()){
            GuestListID = rs2.getString("GuestListID");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    String sqlQuery3 = "SELECT Name FROM guestcontact WHERE GuestListID = '" + GuestListID + "'";
    try {
        rs3 = db.readRequest(sqlQuery3);
        while(rs3.next()){
            ((DefaultListModel)l.getModel()).addElement(rs3.getString("Name"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    db.terminate();
}

提前致谢!

4

2 回答 2

4

请暂时忘记SQL。然后按照以下步骤操作:

  1. 从示例开始--- 对应的Java Web Start 链接
  2. 播放示例,了解如何将元素添加到列表中。
  3. 然后,您需要做的就是获取对要复制的元素的引用,并为每个元素复制它,然后将其添加到第二个列表中。
  4. 享受 Java。

EDIT: I am still looking at your code and I am still confused of what is going on (Please consider making a SSCCE). But let me guess, as something is telling me that when you are calling the ListSelectionListener you are not using getIsValueAdjusting() of the ListSelectionEvent, right? Therefore, this might be as well it, for more read here.

于 2012-06-16T09:15:10.750 回答
2

据我所见,您没有在填充列表之前清除列表,您可以尝试先清除模型,例如:

String sqlQuery3 = "SELECT Name FROM guestcontact WHERE GuestListID = '" + GuestListID + "'";
try {
    rs3 = db.readRequest(sqlQuery3);
    DefaultListModel lm = (DefaultListModel)l;
    lm.clear();
    while(rs3.next()){
        (lm.getModel()).addElement(rs3.getString("Name"));
    }
} catch (SQLException e) {
    e.printStackTrace();
}
于 2012-06-16T09:11:23.817 回答