Alpha numeric sorting of rows in a JTable.

Hi All,

I am using the following class for performing alpha numeric sorting of the rows in my JTable.

import java.util.Comparator;

import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableModel;

/*

* The following class takes in a vector and performs alpha numeric sorting on it

*/

publicclass StepAlphanumComparatorimplements Comparator{

char[] numbers ={'1','2','3','4','5','6','7','8','9','0'};

privateint colIndex;

public StepAlphanumComparator(int colIndex){

this.colIndex = colIndex;

}

privateboolean isIn(char ch,char[] chars){

for (int i = 0; i < chars.length; i++){

if (ch == chars[i])

returntrue;

}

returnfalse;

}

privateboolean inChunk(char ch, String s){

if (s.length() == 0)

returntrue;

char s0 = s.charAt(0);

int chunkType = 0;// 0 = alphabetic, 1 = numeric

if (isIn(s0, numbers))

chunkType = 1;

if ((chunkType == 0) && (isIn(ch, numbers)))

returnfalse;

if ((chunkType == 1) && (!isIn(ch, numbers)))

returnfalse;

returntrue;

}

publicint compare(Object object1, Object object2){

Vector v1 = (Vector)object1;

Vector v2 = (Vector)object2;

Object column1 = v1.get(colIndex);

Object column2 = v2.get(colIndex);

String s1 = (String) column1;

String s2 = (String) column2;

int thisMarker = 0;

int thisNumericChunk = 0;

String thisChunk =new String();

int thatMarker = 0;

int thatNumericChunk = 0;

String thatChunk =new String();

while ((thisMarker < s1.length()) && (thatMarker < s2.length())){

char thisCh = s1.charAt(thisMarker);

char thatCh = s2.charAt(thatMarker);

thisChunk ="";

thatChunk ="";

while ((thisMarker < s1.length()) && inChunk(thisCh, thisChunk)){

thisChunk = thisChunk + thisCh;

thisMarker++;

if (thisMarker < s1.length())

thisCh = s1.charAt(thisMarker);

}

while ((thatMarker < s2.length()) && inChunk(thatCh, thatChunk)){

thatChunk = thatChunk + thatCh;

thatMarker++;

if (thatMarker < s2.length())

thatCh = s2.charAt(thatMarker);

}

int thisChunkType = isIn(thisChunk.charAt(0), numbers) ? 1 : 0;

int thatChunkType = isIn(thatChunk.charAt(0), numbers) ? 1 : 0;

// If both chunks contain numeric characters, sort them numerically

int result = 0;

if ((thisChunkType == 1) && (thatChunkType == 1)){

thisNumericChunk = Integer.parseInt(thisChunk);

thatNumericChunk = Integer.parseInt(thatChunk);

if (thisNumericChunk < thatNumericChunk)

result = -1;

if (thisNumericChunk > thatNumericChunk)

result = 1;

}else{

result = thisChunk.compareTo(thatChunk);

}

if (result != 0)

return result;

}

return 0;

}

}

This is how I use this class...

DefaultTableModel tableModel =new DefaultTableModel();

/* Populate the table ....

....

.....

*/

Vector data = tableModel.getDataVector();

Collections.sort(data,new StepAlphanumComparator(colIndex));

tableModel.fireTableStructureChanged();

Now my problem is when I am using the above class for an Abstract Table Model, it doesn't work, can anyone explain why. If you can give a sample code for making it work for an abstract table model, it would be a bonus.

Thanks,

Knight.

[6962 byte] By [ShymalanKnighta] at [2007-10-2 0:39:47]
# 1

Hi Knight,

first of all, you should post your implementation of the "Abstract Model" you use. As javax.swing.table.AbstractTableModel is abstract itself, you cannot use it directly, but have to create a subclass from it.

Maybe there's something wrong with the way you fill the dataVector, or the way you use the fireXYZ... methods that indicat data change.

One question: Why dou you use the abstract model at all? Maybe it would be easier to subclass the DefaultTableModel.

If you really need to subclass the AbstractTableModel, then you should take a look at DefaultTableModel and get an idea how the data handling is done there. It could be a good idea to stick to the "programming model" of that class.

Greets,

Christian

Christian.Ha at 2007-7-15 16:54:37 > top of Java-index,Desktop,Core GUI APIs...
# 2
why dont you just give me the frickin duke dollars and I tell you the solution
Karan_Guptaa at 2007-7-15 16:54:37 > top of Java-index,Desktop,Core GUI APIs...
# 3
Hi,you can find an example here: http://javaalmanac.com/egs/javax.swing.table/SortCol.html?l=relHope this helps you with your problem.L.P.
lukika at 2007-7-15 16:54:37 > top of Java-index,Desktop,Core GUI APIs...
# 4
> why dont you just give me the frickin duke dollars> and I tell you the solutionWhy should he give you the dolars? You don't know the answer!
sabre150a at 2007-7-15 16:54:37 > top of Java-index,Desktop,Core GUI APIs...
# 5
> > why dont you just give me the frickin duke dollars> > and I tell you the solution> > Why should he give you the dolars? You don't know the> answer!I do !!! The j@ck@$$ OP sits next to my desk .... but , duke dollars dont.
Karan_Guptaa at 2007-7-15 16:54:37 > top of Java-index,Desktop,Core GUI APIs...
# 6

> > > why dont you just give me the frickin duke

> dollars

> > > and I tell you the solution

> >

> > Why should he give you the dolars? You don't know

> the

> > answer!

>

> I do !!! The j@ck@$$ OP sits next to my desk .... but

> , duke dollars dont.

I'm glad I don't work with you!

sabre150a at 2007-7-15 16:54:37 > top of Java-index,Desktop,Core GUI APIs...
# 7
Why simply add a TableSorter to your DefaultTableModel and insert in the JTable the object of the type they are (not only Strings, but Integers, Dates...etc) ?This is a good and fast solution for me
albertthea at 2007-7-15 16:54:37 > top of Java-index,Desktop,Core GUI APIs...