• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help is wanted with MouseAdapter  RSS feed

 
Ben Weng
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a class "TableStructure" extends JPanel. Inside there are two JTables, structureTable (reflecting columns in a database table) and criteriaTable respectively. A mouse listener is registered with the structureTable and if the click count is more than 2, it adds a row in the criteria table.
I observed a weird thing is happening when multiple "TableStructure" objects is added to a scrollable component. The target of the mouse action always falls to the last added "TableStructure" no matter where the action was originated. In more detail, say I have three "TableStructure" added to the scrollpane, I double click on a row in the structureTable of the first "TableStructure" object, a row is added to the criteriaTable in the third "TableStructure" object.
Can anyone explain what I have missed here?

Part of the listener:
 
Ben Weng
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
never mind.
I figured it out. I've setup the structureTable and criterialTable to be class level variables. That's why.

Thanks if you ever given some thoughts.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either create a new listener for each structure panel, initializing each with the source and destination JTables, or use e.getSource() to determine which table pair to use. The first method is cleaner, in my view, and much simpler.
 
Ben Weng
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Unfortunately, the second approach was what I adopted. and it worked. I may try the first one if deadline permits.

Have a good weekend.

b
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!