Solved

Rudimentary question: Adding MouseListener to JTable rows

Posted on 2001-06-11
6
687 Views
Last Modified: 2012-08-14
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.

0
Comment
Question by:pkl_javaq
6 Comments
 
LVL 2

Expert Comment

by:gsatsan
ID: 6179851
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);
0
 

Author Comment

by:pkl_javaq
ID: 6179897
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.
0
 
LVL 2

Accepted Solution

by:
gsatsan earned 25 total points
ID: 6179946
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.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 9

Assisted Solution

by:Ovi
Ovi earned 25 total points
ID: 6180362
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));
 return(v);
}

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);
 return(o);
}
..........................
}

, 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
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6183264
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
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9027563
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.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question