Skip to main content

JTable looses after sort the current selection

6 replies [Last post]
leo_test
Offline
Joined: 2004-04-27
Points: 0

I have a JTable with either a RowSorter or setAutoCreateRowSorter(true) These tables have attached as well a ListSelectionListeners. When I click on the Header to sort the JTable, the table is sorted, but the selection remains in the same row (after the sort). There is as well no ListSelectionEvent which is quite strange.

The problem occurs mainly when I am at the top resp. at the bottom of my Table. When I change the selection afterwards, the content is fine. But I want that the selection keeps track of the sorted-status and therefore the selection should be displayed properly...

Did have anyone similar experiences with incorrect selections? Do I really have to add some additional MouseListeners to the Header (if this would fix the problem)?

?

LeO

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kleopatra
Offline
Joined: 2003-06-11
Points: 0

worksforme - or in other words: time for a small runnable bla-bla .. :-)

CU
Jeanette

leo_test
Offline
Joined: 2004-04-27
Points: 0

I finally figured out, what my problem was: I override my JTable like:

[code]
@Override
public int getSelectedRow() {
int mu = super.getSelectedRow();
if (mu < 0) {
return mu;
}
if (mu >= getRowCount()) {
return mu;
}
return convertRowIndexToModel(mu);
}
[/code]

Which is little bit annoying for the RowSorter which access to that method. Main reason why I override that method is to have a simple sorted table without too much headache about the convertRowIndexTo...

I finally figured out some workaround to the above code. Thx for help.

:)

LeO

kleopatra
Offline
Joined: 2003-06-11
Points: 0

> I finally figured out, what my problem was: I
> override my JTable like:
>
> [code]
> @Override
> public int getSelectedRow() {
> int mu = super.getSelectedRow();
> if (mu < 0) {
> return mu;
>
> if (mu >= getRowCount()) {
> return mu;
> }
> return convertRowIndexToModel(mu);
> }
> [/code]
>
> Which is little bit annoying for the RowSorter which
> access to that method. Main reason why I override
> that method is to have a simple sorted table without
> too much headache about the convertRowIndexTo...
>

DO...OO...OOOOO...NNNNNT!!

All indices, params and return values (naturally the only exceptions are the conversion methods), in the JTable api are in _view_ coordinates, if you override to return something else you are violating super's contract. This is a serious offense (as in nononononneverevernowaydonteventhinkaboutinsecretdreams) which will lead to weird and often hard-to detect misbehaviour (as you experienced ;-).

CU
Jeanette

leo_test
Offline
Joined: 2004-04-27
Points: 0

> DO...OO...OOOOO...NNNNNT!!
>
> All indices, params and return values (naturally the
> only exceptions are the conversion methods), in the
> JTable api are in _view_ coordinates, if you override
> to return something else you are violating super's
> contract. This is a serious offense (as in
> nononononneverevernowaydonteventhinkaboutinsecretdream
> s) which will lead to weird and often hard-to detect
> misbehaviour (as you experienced ;-).
>

Seems like I pushing the pain-button ;) To some extend I agree, to some extent I am stubborn. Several reasons for this approach:

1) From the model I cannot reach the RowSorter resp. convert-methods
2) I use Filter but this implies automatically a Row-Sort (which I didn't requested)
3) The code is already aged and therefore starting corrections would be similar pain as above.
4) The ONLY point where unwanted side-affects arise are in the RowSorter.

Several reasons, which lead me to that approach. Since I have already implemented it with some workaround it is good to know, that I am walking on a razors edge. Gives some nice additional kick ;)

thx

LeO

Typos corrected.

Message was edited by: leo_test

kleopatra
Offline
Joined: 2003-06-11
Points: 0

> it is good to know, that I am walking on a
> razors edge.

hehe ... no, you dug your grave and jumped into it ...

> Gives some nice additional kick ;)

... which now comes from waiting for the earth falling down and burying you

Cheers
Jeanette

leo_test
Offline
Joined: 2004-04-27
Points: 0

> > it is good to know, that I am walking on a
> > razors edge.
>
> hehe ... no, you dug your grave and jumped into it
> ...

with that kind of support I could realy Rest In Peace ;)

:)

LeO