Row selection across multiple tables

Hi

I currently have multiple tables where, if the user selects a particular row or group of rows in one table the correspondign row or group of rows is selected in the other table.

At the moment I've acheived this by placing listSelection listeners on all the tables so that when the selection on one table changes it fires an event and updates the slection on the other tables using:

secondTable.setRowSelectionInterval(

firstTable.getSelectedRows()[0],

firstTable.getSelectedRows()[firstTable.getSelectedRowCount() - 1]

);

This works fine if the user selects a single row or a continuous group of rows. THe problem is if the user selected a non-continuous group, or selects a continuous group and then deselects some of these rows.

Because the method I've got selects all rows between the start and finish of the selection it means rows are highlighted on the other tables that are not highlighted on the first table.

My thoughts on how to approach this would be to cycle through the array of selected rows and select the corresponding rows in the other tables individually rather than selecting a range, but I can't see how to accomplish this. Could anyone give me some pointers or suggestions.

Thanks

[1275 byte] By [Ruanaea] at [2007-11-26 18:17:19]
# 1

This works fine if the user selects a single row or a continuous group of rows. THe problem is if the user selected a non-continuous group, or selects a continuous group and then deselects some of these rows.

What you are doing is correct in principle (use a listener to propagate the selection) but you need to understand the subtleties of the ListSelectionEvent - read the API carefully - it gives you a range of indices which may have changed but does not tell you whether any specific indices in that range are selected or unselected. For that you will need to get the source ListModel from the event and query each row in the event range to determine its selection state.

itchyscratchya at 2007-7-9 5:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 2

What you are doing is correct in principle (use a

listener to propagate the selection)

Always a good start :-)

but you need to

understand the subtleties of the ListSelectionEvent -

read the API carefully - it gives you a range of

indices which may have changed but does not tell you

whether any specific indices in that range are

selected or unselected.

That makes sense, the event just indicates that the selection has changed yes?

For that you will need to get

the source ListModel from the event and query each

row in the event range to determine its selection

state.

This is where I run into difficulty. I think I can go through the each row and find out if its selected or not (using isSelectedIndex(i))

What I'm unsure what to do is how to set that for the rows in the other ListModels. What I would have normally done would be something like

otherListModel.getIndex(i).setSelected(firstListModel.isSelectedIndex(i));

But as the api doesnt list the methods to do that I think I might not have got this concept sorted in my head. Could someone provide me with some pointers?

Thanks

Ruanaea at 2007-7-9 5:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 3

for (int i = min; i <= max; i ++)

{

if (first.isSelectedIndex(i))

{

second.addSelectionInterval(i, i);

}

else

{

second.removeSelectionInterval(i, i);

}

}

FWIW I'm strongly of the opinion that the whole list selection system in Java sucks :o)

itchyscratchya at 2007-7-9 5:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 4

FWIW I'm strongly of the opinion that the whole list

selection system in Java sucks :o)

I'm beginning to agree! Definatly not the easiest thing to do.

Thanks for your help, it now works, so long as I clear the other list models first, making sure I dont end up in a loop :-D

Ruanaea at 2007-7-9 5:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 5

making sure I dont end up in a loop

My normal strategy for avoiding feedback in a mediator which both responds to and causes the same events, is this:

public class SomeMediator implements SomeListener

{

private boolean active = true;

public void someEventHappened(SomeEvent e)

{

if (active)

{

active = false;

// do your stuff here

active = false;

}

}

}

This isn't explicitly thread safe, so it's not suitable for business objects, but since Swing events all occur on the same thread, it is implicitly thread safe when handling any AWTEvent. Happy days :o)

itchyscratchya at 2007-7-9 5:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 6

> if the user selects a particular row or group of rows in one table the correspondign row or group of rows is selected in the other table.

The implication is that the tables have the same number of rows. So just share the SelectionModel (but I think it will work of even if the number of rows is different). Simple example:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=601267

camickra at 2007-7-9 5:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 7

> The implication is that the tables have the same

> number of rows. So just share the SelectionModel (but

> I think it will work of even if the number of rows is

> different).

Yes that works. And now I feel sheepish.

Must learn to look for simple solutions first. Oh well, I now have two different methods to solve the problem. And it might come in useful at some point.

Thanks for all your help guys

Ruanaea at 2007-7-9 5:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 8
Yes that works. And now I feel sheepish.Likewise :o)
itchyscratchya at 2007-7-9 5:50:54 > top of Java-index,Desktop,Core GUI APIs...