我无法从 中删除项目JList
。以下代码已放在JButton
.
DefaultListModel model = (DefaultListModel) list1.getModel();
int selectedIndex = list1.getSelectedIndex();
if (selectedIndex != -1)
{
model.remove(selectedIndex);
}
我无法从 中删除项目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");
}
}
});