0

我无法从 中删除项目JList。以下代码已放在JButton.

 DefaultListModel model = (DefaultListModel) list1.getModel();

     int selectedIndex = list1.getSelectedIndex();
     if (selectedIndex != -1) 
     {
     model.remove(selectedIndex);
     }
4

1 回答 1

2

以下代码应该可以工作

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");
        }
    }
});
于 2013-01-23T06:11:42.467 回答