aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ConcurrentModificationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ConcurrentModificationException" Watch "ConcurrentModificationException" New topic
Author

ConcurrentModificationException

tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
Hi,
I got ConcurrentModificationException when right click(popup) on JList. When i am selecting (right click ) an item from the JList,popup appears. at the same time when i am selecting(right click) another item from the JList i got ConcurrentModificationException.how to solve this problem.any idea

thanks in advance
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Sure. I got an idea....but I won't disclose it because you haven't shown me your code.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
Here is some part of the code

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

JList uses a DefaultListModel by default, which uses a Vector in the background. That class will throw that exception if you are trying to modify it while iterating over it with a (List)Iterator.

The code you pasted most definitely should not cause any problems. The only odd thing is that you are calling SwingUtilities.invokeLater. That will execute the runnable in the Event Dispatcher Thread (EDT), but you're invoking it from that very same EDT.

Can you show us a) the exception stack trace, and b) the parts of your code where you modify the JList or its model?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

tadi raja wrote:. When i am selecting (right click ) an item from the JList,popup appears. at the same time when i am selecting(right click) another item from the JList i got ConcurrentModificationException.


Usually, on a right handed mouse, you left click to select and right click for the context menu (popup). What is it in your case.
Like Rob said, the stack trace is your friend. That will tell you which line of your code is throwing the exception. Do you know how to use a stack trace?
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
I don't know exactly how to use a stack trace .. help me you have any idea ?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Awesome.

You said you got a ConcurrentModificationException. Obviously your IDE/Logs/Console told you.
Copy paste that text here. Do not forget to use the code tags.

A stack trace, typically tells you what type of exception (with some additional information if available), the source file name and even the line number of that source file.
Looking at the line number, you can try and figure out which part of your code is causing problems.
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
Here is the stack trace

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9



This is what caused the exception.
Can you post that chunk of code?
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
here is the code ..


2)
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

You have multiple mouse listeners for the same component?
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
Finally i got the solution .Thanks to Maneesh Godbole for his help .... in my code. Instead of implementing all the listeners in the MouseListener interface, i am using MouseAdapter class to implement a particular listener.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
as a side note,
public void mouseClicked(MouseEvent e) {

never use mouseClicked(), it doesn't always fire.

more info in the apidocs
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

You do know that you can call addMouseListener multiple times, adding a mouse listener each time? It doesn't overwrite any previously added mouse listeners. As such, you don't have to handle multiple mouse listeners yourself.
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
I got ConcurrentModificationException everytime, that means right click on an item when popup displays at the same time when i left click on another item from the list. whenever i right click on an item from the list on the right to bring up a pop up window, then left click on a different item(not on the menu).previous solution was when i use MouseAdapter instead of MouseListener,the problem has been solved.but if i use mouseAdapter instead of MouseListener, lots of dependencies in my code has been corrupted.is there any better idea for this issue.

thanks in advance
 
wood burning stoves
 
subject: ConcurrentModificationException