简体   繁体   English

JList不使用自定义ListModel进行更新

[英]JList not updating with custom ListModel

I have a TestListModel which extends AbstractListModel 我有一个扩展AbstractListModel的TestListModel

public class TestListModel extends AbstractListModel {
List<Test> testlist = new ArrayList<Test>();

public Object getElementAt(int arg0) {
    return testlist.get(arg0);
}

public int getSize() {
    return testlist.size();
}
public void add(Test t) {
    System.out.println("adding");
    testlist.add(t);
    System.out.println(testlist.toString());
}
public void remove(Test t) {
    testlist.remove(t);
}

}

I have a JList like so 我有一个像这样的JList

final TestListModel listModel = new TestListModel();
    listModel.add(new Test("test", "scen"));
    JPanel panel = new JPanel();
    final JList list = new JList(listModel);
    panel.add(list);
    list.setVisibleRowCount(3);
    list.setFont(new Font("Tahoma", Font.PLAIN, 14));
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setBounds(0, 0, 100, 400);

I also have an Add Button that has an actionlister 我还有一个具有动作列表的添加按钮

public void actionPerformed(ActionEvent arg0) {
final JFileChooser fc = new JFileChooser();fc.setCurrentDirectory(new 
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc.showOpenDialog(frmAtt);
if(returnVal == JFileChooser.APPROVE_OPTION) {
    listModel.add(getTest(fc.getSelectedFile().toString()));
}
}

The ArrayList appears to be updating when I add another test through the button but the GUI does not reflect this change. 当我通过按钮添加另一个测试时,ArrayList似乎正在更新,但GUI不反映此更改。 JList appears completely blank. JList显示为完全空白。 It should be showing all the tests in the model. 它应该显示模型中的所有测试。

Only the first test "scen" shows up in the JList which I added manually to the list (can see in the code above). 只有第一个测试“sce”显示在JList中,我手动添加到列表中(可以在上面的代码中看到)。

You're not firing any of the notification methods after changing the ListModel's data. 更改ListModel的数据后,您不会触发任何通知方法。 The solution is to of course do this. 解决方案当然是这样做的。 Look at the AbstractListModel API at the methods that start with fireXXX(...) and call the appropriate one inside of your model whenever data is changed. 在以fireXXX(...)开头的方法中查看AbstractListModel API,并在数据发生更改时调用模型内部的相应API。 ie, in these methods: 即,在这些方法中:

public void add(Test t) {
    testlist.add(t);
    int index0 = testlist.size() - 1;
    int index1 = index0;
    fireIntervalAdded(this, index0, index1);
}

public void remove(Test t) {
    int index0 = ... // this will depend on where t was in the testlist
    int index1 = ... // ditto
    testlist.remove(t);
    fireIntervalRemoved(this, index0, index1);
}

The reason to call these methods is because the model must notify its listeners that the data has been changed, else the listeners (your JList component) will not change their view of the data. 调用这些方法的原因是因为模型必须通知其侦听器数据已更改,否则侦听器(您的JList组件)将不会更改其数据视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM