jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes How do I capture a mouseClicked event from a JTable within a JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I capture a mouseClicked event from a JTable within a JScrollPane" Watch "How do I capture a mouseClicked event from a JTable within a JScrollPane" New topic
Author

How do I capture a mouseClicked event from a JTable within a JScrollPane

Jw Jones
Greenhorn

Joined: Feb 09, 2009
Posts: 16
Greetings, all.

Hopefully there is something simple I'm overlooking, but here goes:

I have a JTable that is within a JScrollPane. If I try to write add a MouseListener to the JTable it never fires. However, a mouse event does fire for the JScrollPane which it is added to. Is there a way to prevent the JScrollPane from capturing the event or passing it along to the JTable?

Sorry if this is a stupid question.

-Jay
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

How have you added your mouse listener? And what does it look like? Because I've added mouse listeners to JTables without any problems, just with "addMouseListener". And just about all my JTables are in JScrollPanes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jw Jones
Greenhorn

Joined: Feb 09, 2009
Posts: 16
Rob Prime wrote:How have you added your mouse listener? And what does it look like? Because I've added mouse listeners to JTables without any problems, just with "addMouseListener". And just about all my JTables are in JScrollPanes.



Thanks for responding, Rob. I'm using the MVC model, so I'll try to give the relevant code snippets:

JPanel on which the JScrollPane & JTable reside(View):



In the Controller class, I have the following code:



Hopefully I'm not leaving out any of the relevant code. I have an action listener on a JButton that's added on the same JPanel in similar fashion that works as expected. The only thing I can see is that the JScrollPane is capturing the mouse listener events before it reaches the table. If I add a mouse listener to the JScrollPane, it works fine. Very confusing.
Jw Jones
Greenhorn

Joined: Feb 09, 2009
Posts: 16
Rob Prime wrote:How have you added your mouse listener? And what does it look like? Because I've added mouse listeners to JTables without any problems, just with "addMouseListener". And just about all my JTables are in JScrollPanes.


Rob, thanks for the quick response. I posted the relevant code by my session timed out before I submitted it & I ended up losing it all. I'll post it again later.

Once again, thanks!

-Jay
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

The following little test works as it should:

Just one question: are you sure that you create an instance of TableController? Because if you add a mouse listener to a JTable inside a JScrollPane, it should catch events. In fact, if I don't add the mouse listener to the table in the above example, nothing happens if you click the table.

By the way, if you want to know which rows and columns are selected, a ListSelectionListener is probably more useful: Just remember to use JTable's own methods for retrieving the selection.
Jw Jones
Greenhorn

Joined: Feb 09, 2009
Posts: 16
Rob Prime wrote:The following little test works as it should:

Just one question: are you sure that you create an instance of TableController? Because if you add a mouse listener to a JTable inside a JScrollPane, it should catch events. In fact, if I don't add the mouse listener to the table in the above example, nothing happens if you click the table.

By the way, if you want to know which rows and columns are selected, a ListSelectionListener is probably more useful: Just remember to use JTable's own methods for retrieving the selection.


No doubt that the TableController is instantiated. I have several ActionListeners & KeyListeners that are working correctly that are controlled by that class. I seem to have no problem with anything but JTable listeners. They appear to be my kryptonite.

I have just started looking at the ListSelectionListener, although so far I am getting the same results.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Hopefully I'm not leaving out any of the relevant code.

you probably have, and posting code typed from memory is never a good idea

class TableMouseListener extends MouseListener {

would be 'implements MouseListener' in your real code.

might be time for you to post a working (not doing as it should) sample program,
so we can see exactly what you're doing
Jw Jones
Greenhorn

Joined: Feb 09, 2009
Posts: 16
Michael Dunn wrote:> Hopefully I'm not leaving out any of the relevant code.

you probably have, and posting code typed from memory is never a good idea

class TableMouseListener extends MouseListener {

would be 'implements MouseListener' in your real code.

might be time for you to post a working (not doing as it should) sample program,
so we can see exactly what you're doing


You're right. I found the source of the issue, although I'm not sure exactly how I'm going to resolve. Thanks for the help, guys.
Jw Jones
Greenhorn

Joined: Feb 09, 2009
Posts: 16
Just to let you guys know, I fixed it using the ListSelectionListener suggested by Rob, although the main problem was my own stupidity.

Thanks for the prompt replies & sorry for wasting time over something that ending up being related to something entirely different.


-Jay
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Jw Jones wrote:although the main problem was my own stupidity.

Just out of curiosity, what was the cause?
Jw Jones
Greenhorn

Joined: Feb 09, 2009
Posts: 16
Rob Prime wrote:
Jw Jones wrote:although the main problem was my own stupidity.

Just out of curiosity, what was the cause?


I have to initialize the JTable early on in the app before I know what data actually needs to populate it. It's at this time that I add the listeners. But, after a selection is made in another table, I repopulate the JTable with a:



which, of course, removes the listeners attachment to the jtable object. So now I store the listeners in variables & add them back after I reinitialize jtable. This might be awkward but it works. I am definitely open to a more elegant solution.

Again, thanks guys for all your input & the promptness with which you delivered it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Instead of creating a whole new JTable, only create a whole new TableModel:
In fact, when you create a JTable the way you do, JTable automatically creates a backing DefaultTableModel for it to use.
Jw Jones
Greenhorn

Joined: Feb 09, 2009
Posts: 16
Rob Prime wrote:Instead of creating a whole new JTable, only create a whole new TableModel:
In fact, when you create a JTable the way you do, JTable automatically creates a backing DefaultTableModel for it to use.


Ahhh. I'll definitely try that. I have used JTables so little that I wasn't aware of that.

Sincere thanks, Rob.

-Jay
Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 186
Jw Jones wrote:
Rob Prime wrote:How have you added your mouse listener? And what does it look like? Because I've added mouse listeners to JTables without any problems, just with "addMouseListener". And just about all my JTables are in JScrollPanes.


Rob, thanks for the quick response. I posted the relevant code by my session timed out before I submitted it & I ended up losing it all. I'll post it again later.

Once again, thanks!

-Jay


Thank you for showing the viewers how to use MouseListener with a table. One correction with your code is that you extend MouseListener and you should implement it because it is an interface and not a class.
 
Don't get me started about those stupid light bulbs.
 
subject: How do I capture a mouseClicked event from a JTable within a JScrollPane