[英]How to get item in a JList and remove it?
我無法從JList
刪除項目。 以下代碼已放置在JButton
。
DefaultListModel model = (DefaultListModel) list1.getModel();
int selectedIndex = list1.getSelectedIndex();
if (selectedIndex != -1)
{
model.remove(selectedIndex);
}
下面的代碼應該工作
JButton removeButton = new JButton("Remove Selected Element");
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int selectedIndex = list1.getSelectedIndex();
if (selectedIndex != -1) {
model.remove(selectedIndex);
} else {
System.out.println("Nothing selected");
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.