Solved

Rudimentary question: Adding MouseListener to JTable rows

Posted on 2001-06-11
6
690 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example using maven 12 105
maven module vs maven project 3 106
jmss example java 2 46
Firefox and firebub 5 22
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses

739 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