Reputation: 11
I have a DefautListModel
which reads file names from a directory. Whenever I have more than 19 files I get:
java.lang.ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 20
I tried ensureCapacity()
for the listModel
.
I tried setVisibleRowCount()
for the Jlist
Any suggestions?
// 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 );
Upvotes: 1
Views: 20