简体   繁体   English

JList不显示所有读取的元素

[英]JList not showing all the elements read

everyone. 大家。 I have a JList inside a JPanel, which I initialized with the following code: 我在JPanel中有一个JList,我用以下代码对其进行了初始化:

DefaultListModel lmodel;
lmodel=new DefaultListModel();
jList1.setModel(lmodel);

JList was created in the Netbeans visual editor and put inside JScrollPane1. JList是在Netbeans可视编辑器中创建的,并放在JScrollPane1中。 When I add or remove a few elements from the model, everything works just fine. 当我从模型中添加或删除一些元素时,一切正常。 Nevertheless, there seems to be a limit to the number of elements the JList can show, which is weirdly set at 294. 但是,JList可以显示的元素数量似乎有一个限制,这是奇怪的设置为294。

I have a method that reads items from a text file, which is pretty standard: 我有一个从文本文件读取项目的方法,这很标准:

BufferedReader inputStream=new BufferedReader(new FileReader(nomeArquivo));
String line = inputStream.readLine();
while (line!=null) {
    int i=line.indexOf("=");
    if (i>0) {
       lmodel.addElement(line);                        
    }
    line = inputStream.readLine();
}         
inputStream.close();
System.out.println("Final list size="+jList1.getModel().getSize());               

All trys and catches are in place - this is just a fragment. 所有的尝试和捕获都到位了-这只是一个片段。 There is no reading error and everything goes well, with no exceptions. 没有读取错误,一切正常,没有例外。

When I read a file that contains 400 elements, all those 400 elements are inside the lmodel. 当我读取包含400个元素的文件时,所有这400个元素都在lmodel内部。 I can even save them afterwards (there is a method for that too) and the println at the end of the method gives me the number 400 as read. 我什至可以随后保存它们(也有一种方法),该方法末尾的println给出读取的数字400。

Nevertheless, the list never shows more that 294 elements. 但是,该列表从未显示超过294个元素。 If I had a number x of elements in the list, only 294-x elements are viewed on screen. 如果列表中有x个元素,则在屏幕上只能查看294-x个元素。

Does anybody know what might be happening? 有人知道会发生什么吗?

Thank you in advance for your help. 预先感谢您的帮助。

Did you put your JList inside of a ScrollPane? 您是否将JList放在ScrollPane中? If you have more than whats visible in the List you should be able to scroll through all 400? 如果您在列表中看不到的东西多了,您应该可以滚动浏览全部400个? Try this somewhere since you didn't post all of your code up 因为您没有发布所有代码,所以请在某个地方尝试一下

JScrollPane scrollPane = new JScrollPane(jList1);

then put the scroll pane onto the panel. 然后将滚动窗格放到面板上。 Or if your using a gui editor remove your old JList and put the scrollpane on and then add the Jlist to the scroll pane. 或者,如果您使用的是gui编辑器,请删除旧的JList并放上滚动窗格,然后将Jlist添加到滚动窗格中。 I hope this helps! 我希望这有帮助!

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

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