actionlisteners on JLists

hey all,

i have a 2D array of JLists created using a for-loop, now i want to select items from one of those lists so i need to know which list the mouse selecting items from, but because the actionlisteners were added using the same for-loop it always tends to return the index of the last created jlist....what should i do to get the index of the list i'm clicking on.

here is a sample of my code:

schedulingListsArray =new JList[coloumns][rows-1];

for(rowCounter=0;rowCounter<rows-1;rowCounter++){

for(coloumnCounter=0;coloumnCounter<coloumns;coloumnCounter++){

schedulingListsArray[coloumnCounter][rowCounter] =new JList(scheduledCourses[scrollPanesNamesCounter-1]);schedulingListsArray[coloumnCounter][rowCounter].addMouseListener(new java.awt.event.MouseAdapter(){

publicvoid mouseClicked(java.awt.event.MouseEvent e){

System.out.println("mouse clicked at " + coloumnCounter +"," + rowCounter);

}

});

>

[1433 byte] By [Hegazya] at [2007-11-27 9:46:20]
# 1

You're accessing a variable inside the anonymous class that's defined outside of that class. If you want to do that, the variable has to be final. Since yours is a loop counter, you can't do that. You could create a final copy of the loop counters inside the loops, and pass those into the anon. class.

Why are you adding MouseListeners to the JLists anyway? Wouldn't a ChangeListener be better? Have you considered using JTable instead of a grid of lists?

hunter9000a at 2007-7-12 23:56:41 > top of Java-index,Java Essentials,Java Programming...
# 2

sorry i'm kind of new at this...i dont really get what you're saying about accessing a variable inside an anonymous class and stuff

i'm using the mouseListeners because i'm creating a scheduling application and i need to move the courses between the lists manually using the mouse, so i need to detect which course in which list is being clicked and dragged

and yes i have tried using JTables but it doesnt help me get the functionalities i desire

thnx again for your help

Hegazya at 2007-7-12 23:56:41 > top of Java-index,Java Essentials,Java Programming...
# 3
MouseListener has a getSource() method.
camickra at 2007-7-12 23:56:41 > top of Java-index,Java Essentials,Java Programming...