Java - Jframe 加载后如何更新 JList

发布于 2024-10-25 10:18:04 字数 833 浏览 7 评论 0原文

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

我有一个带有帐户名称的 Jlist,当选择帐户时,侧面有一个按钮,必须按下该按钮才能调用上面的代码。 该代码应该获取与该帐户关联的联系人姓名并将其填充到 JList。这不会发生。 Jlist 保持空白,我进行了调试,向量 temp 确实获得了 3 个值并将它们存储到新的 jlist 中,问题是,JList 不刷新。

怎样才能让它刷新呢?

非常感谢,我感谢任何帮助。 库纳尔

        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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2024-11-01 10:18:04

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

The new JList you created hasn't been added to any container, so you'd have to add it to the frame, just as the original one was, and then call "validate()" on the frame (always necessary when you add/remove components to a visible window.) But it would be better to call setListData() on the existing JList -- it would update right away, with less flashing around.

奢欲 2024-11-01 10:18:04

您可以使用 ListModel 来操作 JList 内的数据。并且绝对不要在任何步骤创建新的JLists

您已将 JList 绑定到 JFrame。现在您可以使用并修改该模型来获取其中的数据

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

,然后将新模型发回:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文