在Java中清除一个列表

问题描述 投票:1回答:1

我有两个jlist和两个jbuttons。Ok按钮抓取所选项目并将其添加到列表中。然后重做按钮,重新启用Ok按钮并清除列表。错误指的是清除按钮,同时清除列表,系统在Ok按钮处打印列表。然后点击Clear时,Ok被启用,但出现错误。

下面是代码。

public class bcquery extends JPanel implements ActionListener {

    public List<String> feedlist = new ArrayList<String>();
    public List<String> prodlist = new ArrayList<String>();

public bcquery() {

final JButton button1 = new JButton("OK");
        button1.setEnabled(false);

        final JButton button2 = new JButton("CLEAR");

String[] feedStrings = { "a", "b", "c"};
        String[] prodStrings = { "d", "e", "f", "g"}


final JList feedList = new JList(feedStrings);
        feedList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

final JList prodList = new JList(prodStrings);
        prodList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);


button1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                String feedstr = feedList.getSelectedValuesList().toString();

                feedlist = Arrays.asList(feedstr);

                String prodstr = prodList.getSelectedValuesList().toString();
                prodlist = Arrays.asList(prodstr);

                System.out.println(feedlist);
                System.out.println(prodlist);

                button1.setEnabled(false);
            }
        });      

        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {

                feedlist.clear();
                prodlist.clear();
                button1.setEnabled(true);
                 System.out.println(feedlist);
                 System.out.println(prodlist);
            }
        }); 


private static void createAndShowGUI() {
            //Create and set up the window.

            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);
            JFrame.setDefaultLookAndFeelDecorated(true);

            frame.add(new bcquery());
            frame.setResizable(false);


            frame.pack();
            frame.setVisible(true);
        }

        public static void main(String[] args) {
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
            //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.TRUE);


            createAndShowGUI();
                }
            });

    }
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

        }
}

错误:

Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(Unknown Source)
    at java.util.AbstractList$Itr.remove(Unknown Source)
    at java.util.AbstractList.removeRange(Unknown Source)
    at java.util.AbstractList.clear(Unknown Source)
    at biocore.bcquery$4.actionPerformed(bcquery.java:175)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
java arraylist jbutton jlist
1个回答
2
投票

Arrays#asList() 返回一个由指定数组支持的固定大小的列表 -- 任何对 set 行得通,但 remove() 和其他方法将抛出一个 UnsupportedOperationException. 要创建一个可修改的,使用一个以集合为参数的列表构造函数,如 new ArrayList().

所以,把你的代码改成。

feedlist = new ArrayList<String>(Arrays.asList(feedstr));

and:

prodlist = new ArrayList<String>(Arrays.asList(prodstr));
© www.soinside.com 2019 - 2024. All rights reserved.