permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes JPopupMenu and ActionEvents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPopupMenu and ActionEvents" Watch "JPopupMenu and ActionEvents" New topic
Author

JPopupMenu and ActionEvents

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi there. I have created a JPopupMenu that has one menu item on it. It comes up when a user right clicks a row in a JTable to allow them to delete a row. Then a method runs from the table model that deletes the row. The issue I'm running into is the ActionEvent is being executed twice instead of once. The following is the code in the ActionListener:



This is my JPopupMenu class (for the "rrtp" object)


The method deleteRows from the assemblies object (which is a JTable):


The method deleteRows from the tableModel:


What am I doing wrong? I can't seem to figure it out.
Thanks for any help!










Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I had a similar problem once. As it turned out, an action listener was being added twice. And I couldn't figure out why the event was still being executed even though I had removed "the" action listener.

Anyways, I looked at your code and i couldn't point to where this was happening, but maybe you can? is somehow an actionListener being added twice? Is it possible for you to temporarily comment out a line of code that adds an actionlistener?

just a guess. like I said, I can't point to where this is clearly happening.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Genius!! That was exactly what was going on...... I was adding the ActionListener in the constructor and after data was returned to the JTable. I removed the code that was adding the ActionListener in the constructor and it works perfectly now.
Thanks!
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Good to hear. I must say I am experiencing vague feelings of discomfort at this business of adding an action listener "after the fact," but I can't say categorically it's a bad idea, I guess there is a reason for doing it that way.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Well, the app is supposed to allow the delete of a row from the table dependent upon the status of the order when it is returned from the database. Basically they key the order number in, if it has a certain status, then they are allowed to delete from the table otherwise they cannot. After the status is determined is where I'm adding the action listener to the table. (if good status) otherwise I'm removing the listener. What are the repurcussions of doing it this way? Thanks for your input! :-)
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I guess it's more a question of style. If your program is stable, and readable and maintainable, then I guess it's ok. I suppose one repercussion is that it increases the possibility of mistakes with multiple listeners.

My experience with with tables is limited. So I can't really say your approach is wrong, but If it was me I'd at least consider fewer listeners, and when an event does happen, perform some kind of scrutiny of the table to determine what has happened, rather than rely on more sophisticated events to tell me exactly what has happened. Get my drift?

Anyways, take what I say with a grain of salt, cause it sounds like you are more knowledgable about tables than I am.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JPopupMenu and ActionEvents
 
Similar Threads
jtable
JTable can't delete row
Removing rows from DefaultTableModel
JTable
Jtable