我有2个jlist和2个jbuttons。 “确定”按钮可以抓取所选项目并将其添加到列表中。然后重做按钮,重新设置Ok按钮并清除列表。在清除列表时,错误指的是清除按钮。 系统在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)
答案 0 :(得分:2)
Arrays#asList()
返回由指定数组支持的固定大小列表 - 对set
的任何调用都可以,但remove()
和其他方法将抛出UnsupportedOperationException
。要创建可修改的列表构造函数,请使用将集合作为参数的列表构造函数,例如new ArrayList()
。
因此,请将您的代码更改为:
feedlist = new ArrayList<String>(Arrays.asList(feedstr));
和
prodlist = new ArrayList<String>(Arrays.asList(prodstr));