-1

Basically I have this code to print the cell label of each vertex I click of the jgraph. I am trying to store the values of the cells into a string array. I have tried this:

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]) ;
}
                });         
    }

I get these errors when i try to click a vertex:

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)
user2598911
  • 379
  • 2
  • 6
  • 22
  • Every object has a `toString()` method. You should override this, if it doesn't already behave as you want, and call `cell.toString()`. Otherwise there should be a field for the label or method like `getLabel()` to access that field. Apart from that observation, it's very unclear where you are coming from with this question. For example variable `graphComponent` doesn't depend on this section of the code, only on `graph`. And you declared a local variable with `mxGraphComponent graphComponent` which blocks the scope of the outer `graphComponent` (the one `getGraphControl()` was called on) – clwhisk Sep 15 '13 at 20:36
  • 1
    You are right thanks. Dont know why they were left there. Removed them both. I tried storing the cell values clicked in the graph, into an array of strings, but it didnt work. How could I achieve that? – user2598911 Sep 15 '13 at 21:03
  • Well it's your graph...I can tell because it doesn't follow the naming convention for java classes. All you need to store a value into an array of Strings is a String. Following the debug prompts yourself and asking a well defined question are similar skills. Good luck. – clwhisk Sep 15 '13 at 21:23
  • Updated it. I hope that helps more. I tried to convert it to string array but I get the above errors – user2598911 Sep 15 '13 at 22:17
  • You still haven't said what a cell is! – clwhisk Sep 16 '13 at 00:58
  • override `String toString()` in class Vertex, or write and use `String vertexToString(vertex v){...}` Your convertvalue method isn't used right now. – clwhisk Sep 16 '13 at 17:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37469/discussion-between-clwhisk-and-user2598911) – clwhisk Sep 16 '13 at 17:31

1 Answers1

0

Here is the process to resolve such a problem.

at java.util.ArrayList.toArray(Unknown Source)
at GUIquery$2.mousePressed(GUIquery.java:498)`

Line 498 must be

String[] stringArray = objarr.toArray(new String[100]) ;

Check ArrayList documentation for <T> T[] toArray(T[] a)

When called this tries to store ArrayList<Object> members of type Object in a String[]. So it gave an ArrayStoreException. The root of the problem is that the computer has no idea what type cell is. You declared is as an Object, so that's its type. If getCellAt() returns strings, use String cell.

As a note that is not an elegant solution here, if you had an Object obj_str that you knew was a String, you could cast it with

String str = (String)obj_str;
clwhisk
  • 1,805
  • 1
  • 18
  • 17