我有一个DefautListModel
,它从目录中读取文件名。每当我有超过19个文件时,我都会得到:
java.lang.ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 20
我为ensureCapacity()
尝试了listModel
。
我为setVisibleRowCount()
Jlist
有什么建议吗?
// get the file list
String[] newList = getFileList();
// create a list model
listModel = new DefaultListModel<>();
listModel.ensureCapacity( 1000 );
for (String s : newList) {
listModel.addElement(s);
}
// add the model to the jlist
recipeList = new JList(listModel);
recipeList.setName( "recipes" );
recipeList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
recipeList.setLayoutOrientation(javax.swing.JList.VERTICAL);
recipeList.setVisibleRowCount( recipeList.getModel().getSize() );
JScrollPane toScroll = new JScrollPane( recipeList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );