TableSorter :: getting the viewed rownumber

Hi, ive been looking through this forum for over an hour without resolving this issue (which i bet have been answered already a dozen or more times)

im using a search method that later should either select or change the color of the cell matching the search criteria..

and as u might guess, it works nicely until its sorted, then for obvious reasons, since i dont know how to retrieve the viewed row number i end up selecting the same cell/row over and over and over..

so my question is following,

how do i retrieve what rownumber TableModel views the real rownumber on..

and secondly if u feel like it, can u explain how i would change the background of a cell that i know the chords of..

Thanks in advance!

http://java.sun.com/docs/books/tutorial/uiswing/components/examples/TableSorter.java - the TableSorter class im using, no changes made.

[895 byte] By [raindrop__a] at [2007-11-26 17:38:32]
# 1
1. You can search the TableSorter or the original model using getValueAt.2. You can change the cell background by using a custom renderer.Read the tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer
Rodney_McKaya at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 2

the sorting method is already done,

i have no trouble with that..

the problem lies within getting the row where it is displayed after sorting.. the mapped row, or how to put it...

for instance say that row 1 is displayed at row 5 after the sorting, how do i retrieve that 5?

Message was edited by:

raindrop__

raindrop__a at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 3
TableSorter.modelIndexBut as I said if you search the original model there is no need to do that.Because your search will return row 5 and not row 1.
Rodney_McKaya at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 4
i am already searching using the original...so the problem is the reverse..before going here i tried using sorter.modelIndex() to retrieve it, but i guess thats for finding the real row from a viewed row, and not vice versa because it didnt work properly..
raindrop__a at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 5
Just add a modelToView function.You already have the getModelToView() function that you can use.
Rodney_McKaya at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 6

Ok, im not actually sure i know how to write such a method though..

Can anyone help me out doing that?

how should it be built up..

it would be really nice to get that final nail done so that i can leave this program behind me and get some sleep for a change :)

havent slept for 24h so my head isnt really what it should be either..

raindrop__a at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 7
Actually I don't see why you would need to do that.Because the selected row remains selected after you sort your table.Post the code you're using to search the table and select the row you find.And don't forget to use code formatting.
Rodney_McKaya at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 8

public int[] searchTable(String[][] p, int[] mPos, String match) {

boolean found = false;

if ((mPos[0] != 0) || (mPos[1] != 0)) { mPos[1]++; }

if (mPos[1] < p[0].length) {

//run through column once

for (int j = mPos[1]; j < p[0].length; j++) {

System.out.println("for 2 : "+mPos[0]+":"+j );

if (p[mPos[0]][j] != null) {

if (p[mPos[0]][j].equalsIgnoreCase(match)) {

mPos[1] = j;

return mPos;

}

}

}

}

mPos[0]++;

mPos[1] = 0;

if (mPos[0] < p.length) {

for (int i = mPos[0]; i < p.length; i++) {

System.out.println("for 1: "+i);

for (int j = mPos[1]; j < p[i].length; j++) {

System.out.println("for 2 : "+i+":"+j );

if (p[i][j] != null) {

if ((p[i][j].equalsIgnoreCase(match)) && (p[i][j] != null)) {

System.out.println("pos: "+ i+ ":" +j);

if (j+1 < p[i].length)

mPos[0] = i;

mPos[1] = j;

System.out.println("match@" + i + ":" + j);

found = true;

return mPos;

}

}

}

}

}

Arrays.fill(mPos, 0);

return mPos;

}

its a bit messy, but it returns the position mPos[0] - rows and mPos[1] -column, that matched.. the additional code is to make it researchable from last position +1..

anyways, it gets sent the data from MyTableModel mtm as String[][]..

raindrop__a at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 9

For some reason i cant make printscreens, i dont now why..

so i made a little thingie in mspaint to explain what happens

http://hem.bredband.net/viktar/example.GIF

When i first enter paula in to the text field and press search it selects paula, and her row using the method previously posted..

then i sort the column, and press the button again..

and then the selection stays..

If i had used sorter.modelIndex the selection would have changed, but not in a proper fashion..

raindrop__a at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 10
Hello Raindrop,is it possible for you to update to JDK6? There, the tableSorter is so to say included in every Jtable, and you don't need to have an extra sorting class any more. I have an application using this feature, and it works prefectly.RegardsJ鰎g
Joerg22a at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 11

i am running the latest.. atleast afaik,

i downloaded everything new this friday..

either way, it all works nicely..

all apart from this function where i need to figure out how to get the rownumber that row is viewed on, so that i can select the right row when sorting changes as the image..

raindrop__a at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...
# 12

Ok, after getting some food i looked at it once again and noticed that the solution was quite obvious..

ill write it here simply if anyone else has this problem..

First i changed the getModelToView(); from private to public in the TableSorter.java file..

then following gives you the viewRow from modelRow..

// TableSorter sorter = new etc..

...

Int[] theRow = sorter.getModelToView();

viewRow = theRow[modelRow];

so for my task i wrote..

int[] theRow = sorter.getModelToView();

table.changeSelection(theRow[row], col, false, false); }

where row and col are model variables..

raindrop__a at 2007-7-9 0:06:41 > top of Java-index,Desktop,Core GUI APIs...