JList突然改变



在下图中,底部的JComponentJScrollPane内的JList (list)。默认情况下,可见行数是8或10,我不知道。但是突然间,当我对这个项目做更多的改变时,它改变了现在我一次只能看到一个项目(fsgisfg)。如何更改一次显示的行数?

list.setVisibleRowCount(8)不工作

list使用AbstractListModel的扩展作为模型。

主菜单JFrame使用GridBagLayout

我不知道什么可能会发生,因为我甚至试图撤销在项目中做了所有的更改,结果仍然是一样的。

private JList<String> list;
[...]
list = new JList<String>(); 
list.setVisibleRowCount(8); //doesn't change anything
[...]
JScrollPane scroll2 = new JScrollPane(list);
[...]
list.setModel(new BookListModel(library));
list.repaint(); //the model gets the data for the list, and refresh is needed
[...]
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2;
frame.add(scroll2, c);

图像http://dl.dropbox.com/u/71389667/problem.jpg

Emm…现在很难找到导致问题的原因,所以我建议你在继续寻找错误之前先阅读本教程…

至于这一刻,我只是可以假设问题实际上可能来自布局管理器或这个方向的东西…尝试使用JList包含面板(或您使用的组件…)布局管理器。它似乎现在你正在使用某种类型的绝对一个所以改变它,让我们说FlowLayout为例。

作为另一个提示,检查ListModel是否添加了null元素?

对于更多的建议,希望看到更多的代码…


报告是否有帮助

我想我已经找到了问题,但我不知道是什么原因:首先,我的书保存在xml文件在磁盘上的一个文件夹(称为库);现在,如果我删除这个文件夹,以便启用Create Library按钮,则JList看起来是正常大小(显示8行)。即使添加了图书,JList也保持不变,这很好。当我尝试实际更改已创建的xml文件时,问题就出现了,因为默认情况下该文件看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<book name="name" author="author" isbn="isbn" />`

如果我把它变成这样

<?xml version="1.0" encoding="UTF-8"?>
<book name="name" author="" author="" >
<chapter title="chapter1">
    <paragraph>
        <sentence>sentence1</sentence>
        <sentence>sentence1</sentence>
        <sentence>sentence1</sentence>
        <sentence>sentence1</sentence>
    </paragraph>
</chapter>
</book>

JList显示出错

如果我这样写:

<?xml version="1.0" encoding="UTF-8"?>
<book name="fadsf" author="" isbn="" >
    <chapter title="chapter1">
         <paragraph>
            <sentence>sentence1</sentence>
            <sentence>sentence1</sentence>
            <sentence>sentence1</sentence>
    </paragraph>
    </chapter>
</book>

可以正常工作。如果我添加第四个句子标签,事情会变得很糟糕。

最新更新