How to populate JList with data from another JList

Posted by Zhen Le on Stack Overflow See other posts from Stack Overflow or by Zhen Le
Published on 2012-06-16T08:59:37Z Indexed on 2012/06/16 9:16 UTC
Read the original article Hit count: 291

Filed under:
|
|
|

I have a MySQL database which contains data i would like to populate into a JList in my java program. I have two JList, one which is fill with Events Title and the second is to be fill with Guest Name.

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.

I have already successfully populate the first JList with all the Events Title. 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. How can i make it to show only once?

Here is what i got so far...

Java Class

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

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!

© Stack Overflow or respective owner

Related posts about java

Related posts about mysql