How would I create a button that would remove a selected object from my jlist

58 views Asked by At

This is my current attempt

JButton btnNewButton_1 = new JButton("Remove Selected Shape");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultListModel<Node> model = (DefaultListModel<Node>)itemList.getModel();
            Node selectedShape = itemList.getSelectedValue();
            model.removeElement(selectedShape);
            updateList();
        }
    });

I think I had a better approach before, but can not remember.

This is how my list is displayed

JList<Node> itemList = new JList<Node>(model);
    scrollPane_1.setViewportView(itemList);

I have these initialized in my class

private JFrame frame;
private NodePlot plot;
JList<Node> itemList;
JScrollPane scrollPane;
private DefaultListModel<Node> model = new DefaultListModel<Node>();
0

There are 0 answers