Link to home
Start Free TrialLog in
Avatar of pkl_javaq
pkl_javaq

asked on

Rudimentary question: Adding MouseListener to JTable rows

I am adding a MouseListener to the rows of a table in order to add Drag and Drop to the rows (with Apple's D & D API).This is as far as I've gotten:

for (int i = 0; i < table.getRowCount(); i++) {
row[ i ].addMouseListener( rowListener );
}
Which returns the following error:
"...Undefined variable: row
row.addMouseListener( rowListener );
^ "

I [i] think [/i] that what I am looking for is to define "row" using something like:
" row = table.grabAllTheRows "
then add the MouseListener to each row:
"row[ i ].addMouseListener( rowListener )".

If I am on the wrong track, please let me know.

Avatar of gsatsan
gsatsan

yep..you need to define the type of row first and then provide it an object reference..but you can eliminate the row variable by using table.getRow(i) method or it's equivalent in the API.

table.getRow(i).addMouseListener(rowListener);
Avatar of pkl_javaq

ASKER

Hi gsatsan;

Thanks for the comment. I tried it and this code:

for (int i = 0; i < table.getRowCount(); i++) {
table.getRow(i).addMouseListener(rowListener);
}

returns this error message:
"...Method getRow(int) not found in class javax.swing.JTable.
table.getRow(i).addMouseListener(rowListener);"
            ^
How do I fix it?

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of gsatsan
gsatsan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

getRow() won't help. You need it to be Component to add mouse listener. JTable doesn't have components which correspond to rows. Add MouseListener directly to table and use columnAtPoint() and rowAtPoint() to get column/row information.

Regards,
Igor Bazarny
Brainbench MVP for Java 1
www.brainbench.com
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


[split points between gsatsan and Ovi]


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.