我想创建一个JButton
,当我按下它时,它选择JList
的最后一项。
itemsList是我的JList
,我已经有了一些项目
int selectedIndex = itemsList.getMaxSelectionIndex();
itemsList.setSelectedIndex(selectedIndex);
我尝试过这个,但它没有选择任何东西
您的问题是这个方法:getMaxSelectionIndex()
获取JList中当前选择的最大索引(根据JList API)。这不是你想要或想要的东西。相反,你需要获得列表的大小,可以通过列表的模型获得,并从中删除1:
itemsList.setSelectedIndex(itemsList.getModel().getSize() - 1);