简体   繁体   中英

Java - How to update JList once Jframe has been loaded

        Connection connection = newConnection.createConnection();
        Statement newStat = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet res = newStat.executeQuery("SELECT contactName FROM Contact WHERE accountName='"+m+"'");
        Vector<String> temp = new Vector<String>();
        //res.first();
        while (res.next()){
            temp.add(res.getString("contactName"));
        }
        newStat.close();
        connection.close();
        customerContactList = new JList(temp);
        repaint();

I have a Jlist with account names, when an account is selected, on the side there is a button which has to be pressed for in order to call the code above. The code is supposed to get contact names associated to that account and populate them into the JList. This does not happen. The Jlist stays blank, i debugged and the vector temp does get 3 values and stores them into the new jlist, the problem is, JList does not refresh.

How can I make it refresh?

Thanks a lot and I appreciate any help. Kunal

您创建的新JList尚未添加到任何容器中,因此您必须像原始容器一样将其添加到框架中,然后在框架上调用“ validate()”(添加时总是必要的) /将组件移到可见的窗口。)但是最好在现有JList上调用setListData()-它会立即更新,而闪烁的次数较少。

You can use a ListModel to manipulate the data inside the JList . And definitely not create new JLists at any step.

You have your JList bounded to your JFrame . Now you can get the data inside it using

    JList list = new JList();
    ListModel model = list.getModel();

and modify that model, then send the new model back:

    DefaultListModel listModel = new DefaultListModel();
    while (res.next()) {
        listModel.addElement( res.getString("contactName") );
    }
    list.setModel(listModel);

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