Java - 更改对象后更新JList [英] Java - Updating JList after changing an object

查看:158
本文介绍了Java - 更改对象后更新JList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用DefaultListModel的JList。

I have a JList which uses a DefaultListModel.

然后我将值添加到模型中,然后出现在JList中。我创建了一个MouseListener(当双击时)允许用户编辑他们选择的那个人的当前用户号。

I then add values to the model which then appear in the JList. I have created a MouseListener which (when double clicked) allows the user to edit the current user number of that person they have selected.

我已经检查了实际的对象该记录正在改变,它是。我遇到的唯一问题是让实际的Jlist更新以显示该对象的新值。

I have checked that the actual object of that record is being changed, and it is. The only issue I'm having is getting the actual Jlist to update to show the new values of that object.

我当前代码的片段是:

创建JList和DefaultTableModel:

m = new DefaultListModel();
m.addListDataListener(this);
jl = new JList(m);
jl.addMouseListener(this);

更新对象:

String sEditedNumber = JOptionPane.showInputDialog(this, "Edit number for " + name, number);
if (sEditedNumber != null) {
    directory.update (name, sEditedNumber);
}

和(当jl是JList而m是DefaultTableModel时) :

public void contentsChanged(ListDataEvent arg0) {
        jl.setModel(m);
    }


推荐答案

你需要打电话给 fireContentsChanged()

You need to call fireContentsChanged() on the ListModel.

这篇关于Java - 更改对象后更新JList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆