[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

jtable sort issue (urgent)

Posted on 2006-06-02
9
Medium Priority
?
376 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:tbboyett
  • 6
  • 2
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16816418
You can just keep a copy of the data before sorting it
0
 
LVL 5

Author Comment

by:tbboyett
ID: 16816479
>>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
 
LVL 14

Expert Comment

by:hoomanv
ID: 16816577
> The problem is that this data is not sorted, but the jtable is.
sorted jtable is only a view of your real data
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Author Comment

by:tbboyett
ID: 16816709
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
 
LVL 5

Author Comment

by:tbboyett
ID: 16816767
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16817259
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
 
LVL 5

Author Comment

by:tbboyett
ID: 16817639
Excellent CEHJ.  One last thing, how would i make an 'invisible' column in the jtable?  Everything else works great
0
 
LVL 5

Author Comment

by:tbboyett
ID: 16817715
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
 
LVL 5

Author Comment

by:tbboyett
ID: 16818167
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month18 days, 22 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question