JTable sorting

I have a table with 2 columns(names and numbers).I would like to be able to sort the name column alphabetically and the number column numerically in ascending order.thanx
[184 byte] By [shimi1a] at [2007-11-26 16:12:26]
# 1
Read the turorial before posting a question in the forum: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting
Rodney_McKaya at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 2
I have read the tutorial. If you don't know the answer please do not reply!!!
shimi1a at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 3

1. I know the answer.

2. The turorial has an example how to sort table columns.

3. If that's not what you need then you need to give more details in your question, because it's not very clear.

4. Lean how to search in google: http://www.google.com/search?sourceid=navclient&ie=UTF-8&rls=GGLD,GGLD:2004-11,GGLD:en&q=jtable+sort

5. No help for you.

Rodney_McKaya at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 4
I can sort the name column, the number column can't give me 1,2,3,4,5,6 it gives me 1,2,5,3,4,6.
shimi1a at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 5
I can't guess what your code looks like, so If you want further help post a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the problem. http://homepage1.nifty.com/algafield/sscce.html
Rodney_McKaya at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 6
private DeviceTableModel tableModel;tableModel = new DeviceTableModel(deviceTable);TableSorter sorter = new TableSorter( tableModel);sorter.setTableHeader(deviceTable.getTableHeader());deviceTable.setModel(sorter);
shimi1a at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 7
This is not a SSCCE.See my example of a SSCCE for sorting a table model: http://forum.java.sun.com/thread.jspa?threadID=5119628
Rodney_McKaya at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 8

I don't want to interfere the education process here. but, I took the same algorithm and it works fine for numbers.

Make sure that the sorter gets int data from you data model.

you can see that basically it compares objects, so make sure that the objects that get compared are actually numbers.

Noaa at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 9
That's exactly the point.I don't know what data model is being used here, and I'm not going to guess.
Rodney_McKaya at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 10
Rodney I'm just trying to ease your work load so you'll be available for my annoying problems... ;)
Noaa at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 11

> I have read the tutorial.

Where in your question do you state that. Are we suppposed to be mind readers.

> If you don't know the answer please do not reply!!!

Based on the information given it was the most apppriate reply.

> I can sort the name column, the number column can't give me 1,2,3,4,5,6 it gives me 1,2,5,3,4,6.

Where in your original question did you state you could sort Strings but not Integers?

> I don't know what data model is being used here, and I'm not going to guess.

I don't blame you. Usually a problem like this is caused because the table is treating the Integers like a String (because the OP didn't tell the table that the column contains Integers). But since all the OPs data is single digit integers it should make a difference if the data is sorted as a String. So who knows whats wrong.

Of course the OP probably just made up the sort data and that is not really what is happening.

camickra at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 12

> I don't blame you. Usually a problem like this is

> caused because the table is treating the Integers

> like a String (because the OP didn't tell the table

> that the column contains Integers). But since all the

> OPs data is single digit integers it should make a

> difference if the data is sorted as a String. So who

> knows whats wrong.

I know, the default for TableSorter is Alphabetic comparator.

So from the numbers given I can't even guess what is the problem.

Rodney_McKaya at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 13
Thanx for your help guys.It works.
shimi1a at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...
# 14

Also note that you can use a TableSorter without a JTable to sort various tablemodels that you may use (I use tableModels for everything).

TableSorter ts = (TableSorter) dbsys.createTableModel("conn", al, true);

ts.setSortingStatus(1, 1); //sort by company

ts.setSortingStatus(3, 1); //sort by last name

ReportModel rm = new ReportModel (ts); //converts everything to strings and provides an easy mechanism for iterating and accessing the data

Sean

DataVirtuea at 2007-7-8 22:35:04 > top of Java-index,Desktop,Core GUI APIs...