简体   繁体   English

清除 Java 中的列表

[英]Clearing a list in Java

I have 2 jlist and 2 jbuttons.我有 2 个 jlist 和 2 个 jbutton。 The Ok button grabs the selected items and adds them to a list. Ok 按钮抓取所选项目并将它们添加到列表中。 Then the redo button, renables the Ok button and clears the lists.然后重做按钮,重新启用确定按钮并清除列表。 The error refers to the clear button, while clearing the lists.错误是指清除按钮,同时清除列表。 The system prints the lists at Ok button.系统在确定按钮打印列表。 Then when Clear is clicked, Ok is enabled but error comes up.然后当点击清除时,确定已启用但出现错误。

Here is the code:这是代码:

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

        }
}

Error:错误:

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)

Arrays#asList() returns a fixed-size list backed by the specified array -- any call to set will work, but remove() and other methods will throw an UnsupportedOperationException . Arrays#asList()返回一个由指定数组支持的固定大小的列表——任何对set调用都可以工作,但remove()和其他方法将抛出UnsupportedOperationException To create a modifiable one, use a list constructor that takes a collection as argument, such as new ArrayList() .要创建可修改的,请使用将集合作为参数的列表构造函数,例如new ArrayList()

So, change your code to:因此,将您的代码更改为:

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

and:和:

prodlist = new ArrayList<String>(Arrays.asList(prodstr));

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

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