Clicking the table header column programmatically

Hi

For sorting of the JTable I've used the TableSorter API provided in this site. What I need to do is by clicking on another panel the panel containing the JTable should be displayed sorting the respective column. So what I need to do is click the table header column programmatically. Do you know how can I do that.

Thanx.

[344 byte] By [sabbyna] at [2007-11-27 8:41:30]
# 1

Sorting is usually done by first setting the column to sort and then calling the necessary sort method. Since, I dont know which TableSorter class you are using, all I can suggest is to find the method which you can use to set the sortColumn and then the method to initate the sort.

If you where using the TableRowSorter introduced in jdk 5, I'd suggest you check out the toggleSort(int column) method.

ICE

icewalker2ga at 2007-7-12 20:40:30 > top of Java-index,Desktop,Core GUI APIs...
# 2
i'm using the jdk 5 but i didn't find any kind of TableRowSorter class. this class is from jdk 6 i think. the table sorter i used the TableSorter class from http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TableSorterDemoProject/src/components/TableSorter.java
sabbyna at 2007-7-12 20:40:31 > top of Java-index,Desktop,Core GUI APIs...
# 3

Take a look at setSortingStatus function in TableSorter.

If you want ascending sort just call:

setSortingStatus(columnIndex, 1);

This is the code executed when you press table header, it might give you some more insight:public void mouseClicked(MouseEvent e) {

JTableHeader h = (JTableHeader) e.getSource();

TableColumnModel columnModel = h.getColumnModel();

int viewColumn = columnModel.getColumnIndexAtX(e.getX());

int column = columnModel.getColumn(viewColumn).getModelIndex();

if (column != -1) {

int status = getSortingStatus(column);

if (!e.isControlDown()) {

cancelSorting();

}

// Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or

// {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed.

status = status + (e.isShiftDown() ? -1 : 1);

status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}

setSortingStatus(column, status);

}

}

Rodney_McKaya at 2007-7-12 20:40:31 > top of Java-index,Desktop,Core GUI APIs...
# 4

hi

sorting the table is not working. when i do the sorting like this

public void sortColumn(int column, int status){

((TableSorter)table.getModel()).setSortingStatus(column, status);

}

when i sort the table using the above code, the table sorting works fine on clicking the table header, but calling this method outside the panel doesn't sort the table at all.

Instead when i do the sorting like this

public void sortColumn(int col, int status) {

TableSorter temp = new TableSorter(tableModel, true);

table.setModel(temp);

temp.setSortingStatus(col, status);

}

While using the above code the calling this method from other panels works fine but clicking on the table header doesn't work. can u please help me sort out the problem

sabbyna at 2007-7-12 20:40:31 > top of Java-index,Desktop,Core GUI APIs...