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.

Who is Participating?
Swing does not provide with any functionality for trapping mouse motion in a JTable. You could however have a work around. You could add a component/panel in each of the cells and add a mouselistener to those components.
gsatsanCommented: 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.

pkl_javaqAuthor Commented:
Hi gsatsan;

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

for (int i = 0; i < table.getRowCount(); i++) {

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

Thanks in advance.
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

You can implement your own getRow() like this :

suppose your table class is called MyTable :

public class MyTable extends JTable {
private MyTmodel
public Vector getRow(int row) {
 Vector v = new Vector();
 int cols = getColumnCount();
 for(int i = 0; i<cols; i++)
  v.addElement(getValueAt(row, i));

public Object[] getRow(int row) {
 int cols = getColumnCount();
 Object[] o = new Object[cols];
 for(int i = 0; i<cols; i++)
  o[i] = getValueAt(row, i);

, and so you have two methods which are returning a roe Vector or an row array elements of Objects.
 The same method you can use for "paste" a row, using setValueAt(int row, int col) method
Igor BazarnyCommented:

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.

Igor Bazarny
Brainbench MVP for Java 1
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.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.