Reputation: 81
Is it possible to prevent sorting all together on the JTable? Basically I don't want anything to happen when the user clicks the table header, and for the content to be in a static order.
Upvotes: 2
Views: 2061
Reputation: 1505
The best and simple way to disable sorting when user clicks on any table header columns:
insert this line of code
yourTableVariable.setRowSorter(null);
Practical example:
yourTableVariable.getTableHeader().addMouseListener(new MouseAdapter() //here you make the click avaible ONLY on Table Header
{
@Override
public void mouseClicked(MouseEvent arg0)
{
if (SwingUtilities.isLeftMouseButton(arg0)) //here you select the mouse left click action
{
yourTableVariable.setRowSorter(null); //here is disableing the sorting
}
}
});
Upvotes: 0
Reputation: 109813
Basically I don't want anything to happen when the user clicks the table header, and for the content to be in a static order.
basically JTable
haven't any Sorter, you have to remove codelines
- JTable#setAutoCreateRowSorter(true);
- table.setRowSorter(sorter);
- custom Comparator added as MouseEvent to the JTableHeader
have look at and read JTable tutorial about Sorting and Filtering
Upvotes: 2
Reputation: 9875
See the Javadoc:
public void setRowSorter(RowSorter sorter)
Parameters:
sorter
- theRowSorter
;null
turns sorting off
Upvotes: 4