简体   繁体   English

当从ArrayList中删除项目时,如何从JList中删除项目?

[英]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 我有一个名为slistObjectsArrayList

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 (不过,不仅仅是一个sitem 。我将sitem toString打印到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. 当用户从Jlist删除某些内容时,我也需要删除相应的slist Item。 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. 通过使用getSelectedIndex()获取JList项目的索引,然后使用该索引在ArrayList中删除该项目。 Make sure if nothing is selected getSelectedIndex() will return -1. 确保如果未选择任何内容,则getSelectedIndex()将返回-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. 您需要侦听列表中的click事件,然后从该事件中获取选定的索引,并相应地修改列表。

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. 这是一个简单的示例,将使您有一个想法:不要太担心我的IDE生成的initComponents()方法。 The key thing is to look at the event listener in the main method: 关键是要在main方法中查看事件侦听器:

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);
      }
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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