Java - Jframe 加载后如何更新 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();
我有一个带有帐户名称的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您创建的新 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.
您可以使用 ListModel 来操作
JList
内的数据。并且绝对不要在任何步骤创建新的JLists
。您已将
JList
绑定到JFrame
。现在您可以使用并修改该模型来获取其中的数据,然后将新模型发回:
You can use a ListModel to manipulate the data inside the
JList
. And definitely not create newJLists
at any step.You have your
JList
bounded to yourJFrame
. Now you can get the data inside it usingand modify that model, then send the new model back: