Solved

Rudimentary question: Adding MouseListener to JTable rows

Posted on 2001-06-11
6
693 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

617 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