FULL PRODUCT VERSION :
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
There is no way to temporally disable sorting from the table header. For instance, when a user has sorted a table, made some changes and then wants save those changes to a datastore it would be nice to block the user from altering the JTable state during the save. Since the save happens in a relatively short time it makes sense to simply disable the table to lock the selection and disable the table header to prevent sorting via mouse clicks to the header. Removing the row sorter would cause all filters and sort order changes to be undone. Also the user might want to perform additional sorting and filtering after the save is done.
If the header is not visible the user can not sort via header so if it is not enabled it should also not allow sorting.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a JTable with a visible header.
Add a RowSorter
Disable the JTableHeader.
Click the column header.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Sorting via the header should not work if the header itself is disabled. The enabled state of the actual JTable should not matter, only the headers enabled state.
ACTUAL -
Sorting via the header always works when a RowSorter is installed.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
public class HeaderSort extends MouseAdapter implements Runnable {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(createModel());
table.setAutoCreateRowSorter(true);
frame.setContentPane(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
JTableHeader header = table.getTableHeader();
header.setEnabled(false);
header.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
System.out.println(e.getComponent().isEnabled());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new HeaderSort());
}
private static final TableModel createModel() {
Vector cols = new Vector();
cols.add("A");
Vector data = new Vector();
Vector row = new Vector();
row.add("A");
data.add(row);
row = new Vector();
row.add("Z");
data.add(row);
DefaultTableModel model = new DefaultTableModel(data, cols);
return model;
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Toggle the visiblity property of the header but no headers show up (as expected).
Disable all columns if RowSorter is a DefaultRowSorter. Requires caller to remember previous column state.