[英]Java in actionPerformed cannot add to arrayList, result is “AWT-EventQueue-0” java.lang.UnsupportedOperationException
Thanks! 谢谢! This problem is solved by new ArrayList.
通过新的ArrayList解决了此问题。 phrases = new ArrayList(Arrays.asList(s));
短语=新的ArrayList(Arrays.asList(s));
First, thank-you stackoverflow.com, for having such good information and help! 首先,谢谢您stackoverflow.com,提供了这么好的信息和帮助! Below is my minimum failing case.
以下是我的最小失败案例。 I am running java jdk 1.7.0_60 from a cygwin window on Windows 7. The problem is line 60 inside
actionPerformed
where I try to do: phrases.add("QUIT;");
我正在Windows 7上的cygwin窗口中运行java jdk 1.7.0_60。问题是在
actionPerformed
内的第60行,我尝试在其中执行以下操作: phrases.add("QUIT;");
where phrases is an array list. 短语是数组列表。 Although work-around is to append
QUIT
to JTextArea
, I would like to understand why the phrases.add()
does not work. 尽管解决方法是将
QUIT
附加到JTextArea
,但我想了解为什么phrases.add()
无法正常工作。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
class MFC extends JFrame implements ActionListener {
JTextArea myTextArea = new JTextArea(12, 30);
//same result if public or private
private List<String> phrases = new ArrayList<String>(Arrays.asList(
"Type or paste your SQL here","quit;"));
MFC() {
super("List<String>");
setSize(370, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
setLayout(flow);
JPanel row1 = new JPanel();
myTextArea.setLineWrap(true);
myTextArea.setWrapStyleWord(true);
// initialize the text area from initial phrases
for(int i=0; i < phrases.size(); i++) {
myTextArea.append(phrases.get(i) + "\n");
}
//http://stackoverflow.com/questions/23701113/keeping-the-text-in-a-java-textfield-always-selected
//Make sure the new text is visible, even if there
//was a selection in the text area.
myTextArea.setCaretPosition(myTextArea.getDocument().getLength());
JScrollPane scroll = new JScrollPane(myTextArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
row1.add(scroll);
add(row1);
JPanel row2 = new JPanel();
JButton execute = new JButton("Execute");
execute.addActionListener(this);
row2.add(execute);
add(row2);
setVisible(true);
} // end MFC()
public void actionPerformed(ActionEvent evt) {
myTextArea.append("QUIT;"); //this works
String s[] = myTextArea.getText().split("\\n"); //this works
phrases = Arrays.asList(s); //this works
dump();
/*** THIS IS THE STATEMENT NOT WORKING ***/
phrases.add("QUIT;"); // NOT WORKING
dump();
}//end actionPerformed()
public static void main(String[] arguments) {
MFC mfc = new MFC();
}
void dump() {
System.out.println("phrases.size() is " + phrases.size());
Iterator iterator1 = phrases.iterator(); // single use
while (iterator1.hasNext()) {
System.out.println("Phrase " + iterator1.next()); // works
}
}
}//end MFC class
Here is the result: 结果如下:
$ java MFC
phrases.size() is 5
Phrase abc
Phrase def
Phrase 123
Phrase 456
Phrase QUIT;
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at MFC.actionPerformed(MFC.java:60)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
You've assigned your List
variable phrases
to Arrays.asList
which returns a fixed size List
您已将
List
变量phrases
分配给Arrays.asList
,它返回固定大小的List
phrases = Arrays.asList(s);
Since elements can be added to an ArrayList
you could just use 由于可以将元素添加到
ArrayList
您可以使用
phrases = new ArrayList<>(Arrays.asList(s));
as you did originally. 就像您最初所做的那样。
Arrays.asList()
doesn't create a new list, really. Arrays.asList()
不会创建新列表。 It gives you an object that acts like a List
, but really just accesses the array. 它为您提供了一个类似于
List
的对象,但实际上只是访问数组。 Since you can't make an array larger by adding elements to it, you can't append to this kind of list, either. 由于无法通过向其中添加元素来使数组变大,因此也无法将其追加到此类列表中。
Instead of 代替
phrases = Arrays.asList(s);
try 尝试
phrases = new ArrayList<String>(Arrays.asList(s));
which does create a new list, and initializes it by copying the elements of the array. 确实会创建一个新列表,并通过复制数组的元素对其进行初始化。
Read the javadoc : 阅读javadoc :
public static List asList(T... a)
公共静态列表asList(T ... a)
Returns a fixed-size list backed by the specified array.
返回由指定数组支持的固定大小的列表。
(emphasis mine) (强调我的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.