Hi, all,
I have a customized JTable and I use the Java 6.0 feature:
table.setAutoCreateRowSorter(true)
to enable auto sorting on rows.
So when I add a new row to the table if the auto rowSorter is turned
on for a certain column (after I clicked on one column header to
enable the rowSorter), the new row will automatically jump to the
position according to the sorting result.
But I want the new row always at the end of the table when added. So I
want to turn off the auto sorting when adding a new row. I check the
java document and find the only possible method I can all on JTable,
or tableModel is
table.setAutoCreateRowSorter(false);
I add the line in my table.addRow() method but it doesn't work, the
sorter still executes.
public void addRow (MyObject rowData){
this. setAutoCreateRowSorter(false);
this.getModel.addRow(rowData);
}
I wonder how can I achieve my goal. i.e. use the autoRowSorter given
by JTable but disable it when new row is added to my table untill next
time user click on a column to activate the sorter again.
BTW, The doc for the setAutoCreateRowSorter() method is:
/**
* Specifies whether a {@code RowSorter} should be created for the
* table whenever its model changes.
* <p>
* When {@code setAutoCreateRowSorter(true)} is invoked, a {@code
* TableRowSorter} is immediately created and installed on the
* table. While the {@code autoCreateRowSorter} property remains
* {@code true}, every time the model is changed, a new {@code
* TableRowSorter} is created and set as the table's row sorter.
*/
It doesn't tell clearly about what happen or when should set it to
false.
Ray Ma - 16 Aug 2007 01:30 GMT
I have a walk around that can disable the RowSorter. I find one tricky
thing is that even if you call:
table.getRowSorter().setSortable(columnIndex,false);
if you have called:
setSortable(columnIndex,true);
previously, it's actually still using the last sorting rule that you
apply to your table column, when the column sortable set to true.
e.g. If you have a quantity field and you click once: sort in
ascending order; twice: sort in descending order; then you call
setSortable(colIndex,false) and you add a new row, you'll find you can
no longer click on the table header to sort a column, BUT if you add a
new row, it is still added using descending order.
The right way is to use: table.setRowSorter(null) when you want to
disable column sorting.
The following code can achieve:
3 clicks on table header,1st: sort ascendingly, 2nd sort descendingly,
3rd disable sorter
class MyTable extends JTable{
int headerClickCount =0;
private TableRowSorter<XXTableModel> sorter =null;
MyTable(TableModel tableModel){
super(tableModel);
......
sorter = new TableRowSorter(this.getTableModel());
}
/**add mouse listener for mouse clicking on table header*/
public void addMouseListenerForHeader(){
this.getTableHeader().addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
headerClickCount++;
if(headerClickCount==3){
MyTable.this.setRowSorter(null);
headerClickCount=0;
}
else{
if(MyTable.this.getRowSorter()==null)
sorter.setModel(MyTable.this.tableModel);//
Must reset tableModel to the sorter
MyTable.this.setRowSorter(sorter);
}
}
});
}