简体   繁体   中英

How do you remove an item from a JList while removing an Item from an ArrayList?

I have an ArrayList of Objects called slist filed with

Store sitem1 = new Store("Movie", 20.00, 500, 0, 0.00);

(More than just one sitem though. I have the sitem toString being printed to a JList

 for (int i = 0; i < customer.slist.size(); i++) {
        dlm.addElement(customer.slist.get(i).toString());//slist is an array of objects.
    }
    checkOutList.setModel(dlm);

When the user removes something from the Jlist I need the corresponding slist Item to be removed as well. I have no clue how to do it though.

get index of JList item by using getSelectedIndex() then using that index remove the item in ArrayList. Make sure if nothing is selected getSelectedIndex() will return -1.

You need to listen for the click event on the list, and then get the selected index from the event, and modify your list accordingly.

You also need to be careful because modifying your list will fire another event, and you need to make sure you're handling just the first one, so you need to remove the listener while you modify your model.

Here's a simple example that will give you the idea Don't worry too much about the initComponents() method, that was generated by my IDE. The key thing is to look at the event listener in the main method:

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class TestJFrame extends javax.swing.JFrame {

  public TestJFrame() {
    initComponents();
  }

  private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
  }                      

  public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {

        final DefaultListModel dlm = new DefaultListModel();
        final List<String> items = new ArrayList<String>();
        items.add("apple");
        items.add("pear");
        items.add("banana");

        for (String item : items) {
          dlm.addElement(item);
        }

        final JList checkOutList = new JList(dlm);
        checkOutList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        checkOutList
                .addListSelectionListener(new ListSelectionListener() {
                  @Override
                  public void valueChanged(ListSelectionEvent e) {
                    if (e.getFirstIndex() != -1 && !e.getValueIsAdjusting()) {
                      int firstIndex = e.getFirstIndex();
                      checkOutList.removeListSelectionListener(this);
                      checkOutList.clearSelection();
                      dlm.remove(firstIndex);
                      items.remove(firstIndex);
                      checkOutList.addListSelectionListener(this);
                    }
                  }
                }
                );

        TestJFrame frame = new TestJFrame();
        frame.setSize(200, 200);
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setSize(200,200);
        panel.add(checkOutList);
        frame.add(panel);
        checkOutList.setSize(200, 200);
        frame.setVisible(true);
      }
    });
  }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM