I am unable to remove an item from JList
. The following code has been put on the JButton
.
DefaultListModel model = (DefaultListModel) list1.getModel();
int selectedIndex = list1.getSelectedIndex();
if (selectedIndex != -1)
{
model.remove(selectedIndex);
}
The following code should work
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");
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.