简体   繁体   English

如何用另一个JList中的数据填充JList

[英]How to populate JList with data from another JList

I have a MySQL database which contains data i would like to populate into a JList in my java program. 我有一个MySQL数据库,其中包含我想填充到Java程序中的JList中的数据。 I have two JList, one which is fill with Events Title and the second is to be fill with Guest Name. 我有两个JList,一个用事件标题填充,第二个用来宾名称填充。

What i would like is when the user click on any of the Events Title, the second JList will show all the Guest Name that belong to that Event. 我想要的是当用户单击任何事件标题时,第二个JList将显示属于该事件的所有来宾名称。

I have already successfully populate the first JList with all the Events Title. 我已经成功用所有事件标题填充了第一个JList。 What I'm having trouble with is when the user click on the Events Title, the Guests Name will show twice on the second JList. 我遇到的麻烦是,当用户单击事件标题时,来宾名称将在第二个JList上显示两次。 How can i make it to show only once? 我怎样才能只显示一次?

Here is what i got so far... 这是我到目前为止所得到的...

Java Class 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;
}

Controller Class 控制器类

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

Class with all the sql statement 具有所有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();
}

Thanks in advance! 提前致谢!

Please for the time being forget about the SQL . 请暂时不要使用SQL Then follow the steps below: 然后按照以下步骤操作:

  1. Start with the example --- the corresponding Java Web Start link . 示例开始---相应的Java Web Start链接
  2. Play with the sample, to see how to add elements to a list. 玩示例,以了解如何将元素添加到列表中。
  3. Then all you need to do is grab a reference to the one you want to copy and for each element copy it then add to the 2nd list. 然后,您所需要做的就是获取要复制的参考,并为每个元素复制一个参考,然后将其添加到第二个列表中。
  4. Enjoy Java. 享受Java。

EDIT: I am still looking at your code and I am still confused of what is going on (Please consider making a SSCCE ). 编辑:我仍在查看您的代码,我仍然对正在发生的事情感到困惑(请考虑使用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? 但是,让我猜,因为东西是告诉我,当你调用ListSelectionListener你不使用getIsValueAdjusting()中的ListSelectionEvent ,对不对? Therefore, this might be as well it, for more read here . 因此,也可能如此, 有关更多信息,请参见此处

As far as I can see, you aren't clearing the list before populate it, you could try to clear the model first, like: 据我所知,您不会在填充列表之前清除列表,可以尝试首先清除模型,例如:

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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