Reputation: 36640
based on this post I have implemented a handy feature to drag and drop rows in a JTable to re-order them. The feature is attached to the whole table however, and I would like to attach it only to one column (displayed as an icon) so that the mouse events don't get affected in other columns.
The whole row is to be dragged/re-ordered, however I want to use a specialised icon as the 'handle' for dragging which would appear in the far left or far right column. I have seen this concept done before (not in java) but can't find a suitable example right now.
Currently, the drag handler is installed like this:
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setDragEnabled(true);
table.setDropMode(DropMode.INSERT_ROWS);
table.setTransferHandler(new ReorderRowTransferHandler(table));
Is there a way to attach the drag handler to a single column only?
EDIT: @Kleopatra's answer is good, however part of my problem is that my other columns contain comoponents such as buttons which due to the drag handler, no longer show their 'pressed' state. i'm hoping to find a solution which restricts the drag mouse handler so that it impacts the first column.
Upvotes: 0
Views: 261
Reputation: 51525
To start dnd only if the mouse drag is initiated in the (f.i.) the first column of the table, implement the exportAsDrag method of the custom TransferHandler to return NONE elsewhere, something like:
@Override
public void exportAsDrag(JComponent comp, InputEvent e, int action) {
if (e instanceof MouseEvent) {
MouseEvent mouse = (MouseEvent) e;
if (table.columnAtPoint(mouse.getPoint()) != 0) {
action = NONE;
}
}
super.exportAsDrag(comp, e, action);
}
Upvotes: 2