jtable sort issue (urgent)

I am using the code found at this website to be able to sort a jtable's data
http://www.objects.com.au/java/examples/swing/SortableTable.do

The problem that I am having is that after sorting the table by clicking on one of the headers, I grab the data from the jtable by using table.getDataVector();
The problem is that this data is not sorted, but the jtable is.

How can I determine what was selected from the jtable after it has been sorted by clicking on the header and still be able to pull the the data from the vector that matches that selection?

If it helps I add the data to the table by doing the following:
((SortableTableModel)table.getModel()).setColumnIdentifiers(headers);
SortableTableModel t = (SortableTableModel)table.getModel();

// start a loop here and create a seperate vector for each row
Vector row = new Vector();
data.addElement("B");
data.addElement(getDate("98/12/02"));
data.addElement(new Integer(14));
data.addElement(new Boolean(false));
t.addRow(data);
// end  the loop


Any suggestions would be very much appreciated
LVL 5
tbboyettAsked:
Who is Participating?
 
CEHJCommented:
Sorry yes - i see the problem. You can use a unique index column to enable you to identify the row. If you don't already have one in the table, you can add an 'invisible' one as the end column of the model, which you can then read.

Let's say you sort and then select the fourth row. You could then do

Vector row = (Vector)((DefaultTableModel)table.getModel()).getDataVector().get(3); // 4th row
// Now get the original index
int indexInModel = ((Integer)row.get(row.size() - 1)).intValue();
0
 
CEHJCommented:
You can just keep a copy of the data before sorting it
0
 
tbboyettAuthor Commented:
>>You can just keep a copy of the data before sorting it

Hey CEHJ, not sure i understand.  I keep a copy of the unsorted data in a Vector but if i sort the jtable and the user selects say row 4 of the jtable, it's likely that position 4 in the jtable isn't going to be the same as element at position 4 in the vector.

I may be misunderstanding, could you elaborate a little please.  Thanks
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
hoomanvCommented:
> The problem is that this data is not sorted, but the jtable is.
sorted jtable is only a view of your real data
0
 
tbboyettAuthor Commented:
ok since the jtable is just a view of the data I'm assuming i will need to sort the data at the same time that i sort the jtable?

If i need to sort the data I will probably need to use a comparator of some sort.  To be honest, the comparator confuses me.
Do either of you know of a simple way to sort the data?  it is a vector of vectors, in other words a vector of rows.  Each row contains distance, direction, city, zip, and count.  Example of the data below:

Vector data  = new Vector();
Vector row = new Vector();
data.addElement(new Double(5.23));
data.addElement("NW");
data.addElement("Atlanta");
data.addElement(new Integer(30005));
data.addElement(new Integer(255));
data.addElement(row);
0
 
tbboyettAuthor Commented:
I made an error above it should be

Vector data  = new Vector();
Vector row = new Vector();
row.addElement(new Double(5.23));
row.addElement("NW");
row.addElement("Atlanta");
row.addElement(new Integer(30005));
row.addElement(new Integer(255));
data.addElement(row);

sorry about that
0
 
tbboyettAuthor Commented:
Excellent CEHJ.  One last thing, how would i make an 'invisible' column in the jtable?  Everything else works great
0
 
tbboyettAuthor Commented:
I appologize, I'm back to the same issue if i do this.  The reson is because once it's sorted the getDataVector() does Not get sorted, therefore i'm still grabbing the 4th row from the original data not the sorted data
0
 
tbboyettAuthor Commented:
I figured it out by using the comparator and sorting it in asc or desc order at the same time that i sort the jtable and it works perfectly.

Thanks you guys helping as usual
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.