Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #2254 > unrolled thread
| Started by | "Ray Ma" <ray.ma@THRWHITE.remove-dii-this> |
|---|---|
| First post | 2011-04-27 15:38 +0000 |
| Last post | 2011-04-27 15:38 +0000 |
| Articles | 2 — 1 participant |
Back to article view | Back to comp.lang.java.gui
where to use setAutoCreat "Ray Ma" <ray.ma@THRWHITE.remove-dii-this> - 2011-04-27 15:38 +0000
Re: where to use setAutoC "Ray Ma" <ray.ma@THRWHITE.remove-dii-this> - 2011-04-27 15:38 +0000
| From | "Ray Ma" <ray.ma@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:38 +0000 |
| Subject | where to use setAutoCreat |
| Message-ID | <1187206548.935996.282280@q4g2000prc.googlegroups.com> |
To: comp.lang.java.gui
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.
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [next] | [standalone]
| From | "Ray Ma" <ray.ma@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:38 +0000 |
| Subject | Re: where to use setAutoC |
| Message-ID | <1187224253.457074.20520@e9g2000prf.googlegroups.com> |
| In reply to | #2254 |
To: comp.lang.java.gui
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);
}
}
});
}
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.gui
csiph-web