基本上我有这个代码来打印我点击jgraph的每个顶点的单元格标签。我试图将单元格的值存储到字符串数组中。我试过这个:
graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
ArrayList<Object> objarr = new ArrayList<Object>() ;
if (e.getButton() == 3 && e.getClickCount() == 1) {
long x = e.getX();
long y = e.getY();
Object cell = graphComponent.getCellAt((int) x, (int)y);
System.out.println(graph.convertValueToString(cell));
objarr.add(cell);
}
String[] stringArray = objarr.toArray(new String[100]) ;
}
});
}
当我尝试点击顶点时出现这些错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.toArray(Unknown Source)
at GUIquery$2.mousePressed(GUIquery.java:498)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(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 :(得分:0)
以下是解决此类问题的过程。
at java.util.ArrayList.toArray(Unknown Source)
at GUIquery$2.mousePressed(GUIquery.java:498)`
第498行必须
String[] stringArray = objarr.toArray(new String[100]) ;
检查ArrayList documentation <T> T[] toArray(T[] a)
调用时,会尝试将ArrayList<Object>
Object
成员存储在String[]
中。所以它给了ArrayStoreException。问题的根源是计算机不知道类型 cell
是什么。您声明为Object
,因此它的类型。如果getCellAt()
返回字符串,请使用String cell
。
作为一个在这里不是优雅解决方案的说明,如果您知道的Object obj_str
是String
,则可以使用
String str = (String)obj_str;