How do I capture a mouseClicked event from a JTable within a JScrollPane
Jw Jones
Greenhorn
Joined: Feb 09, 2009
Posts: 16
posted
0
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?
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 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:
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
posted
0
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.
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
posted
0
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
posted
0
> 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
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
posted
0
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.
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
posted
0
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.