I had the same problem and I was looking for it. So far I have found two ways to do this.
- The "if I rewrote it myself" method : changing base classes from Java.
TableColumn will need a new property, for example, "resizingAllowed", for this, "reorderingAllowed" is required. It follows that changes occur in BasicTableHeaderUI :
Has already:
private static boolean canResize(TableColumn column, JTableHeader header) { return (column != null) && header.getResizingAllowed() && column.getResizable(); }
This is also needed:
private static boolean canMove(TableColumn column, JTableHeader header) { return (column != null) && header.getReorderingAllowed() && column.getReorderable(); }
(Note: if you do not want the first column not to move, you can do without changing the TableColumns:
private static boolean canMove(TableColumn column, JTableHeader header) { return (column != null) && header.getReorderingAllowed() && header.getColumnModel().getColumnIndex(column.getIdentifier()) != 0; }
)
After two places to change in MouseInputListener :
- in
mousePressed by calling canMove() instead of header.getReorderingAllowed() . This ensures that there is no column that should not be moved. But this is not enough, we need to prevent the movement of fixed columns while dragging another. You also need to change mouseDragged when it gets "newColumnIndex":
if (0 <newColumnIndex && newColumnIndex <cm.getColumnCount ())
You need to add a condition if this new index can be moved, for example, using the canMove () method. Thus, when you drag a column onto this fixed one, you will still drag it, but it will not change them.
Note that for this method, you will need to explicitly configure the user interface for the JTableHeader used for your JTable, which is actually not ideal. But this is most adapted, as it concerns the problem where it should be.
- "Let's try to block normal behavior with what we have . " Without changing the user interface, this method focuses on the JTableHeader to block commands created by the user interface.
First, to block the drag and drop of the first column, we need a subclass of JTableHeader with this overridden method:
@Override public void setDraggedColumn(TableColumn pAColumn) { int lIndex = -1; if (pAColumn != null) lIndex = getColumnModel().getColumnIndex(pAColumn.getIdentifier()); if (lIndex != 0) super.setDraggedColumn(pAColumn); }
This will prevent the user from dragging the first column. But, as described above, this is only one part of the problem, we need to prevent the replacement of another shuffled column with this first one.
So far, I do not have the correct method for this. I tried to subclass TableColumnModel and override the moveColumn() method:
@Override public void moveColumn(int pColumnIndex, int pNewIndex) {
But this will not work, since the user interface will still update the mouse position in the mouseDragged method, you will have a transition from the dragged column to another location.
So, I'm still looking and wondering if anyone has any suggestions for this part.