Solved

Rudimentary question: Adding MouseListener to JTable rows

Posted on 2001-06-11
6
681 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

Assisted Solution

by:Ovi
Ovi earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now