-1

I can only disable drag and drop for the entire table, but I need to be able to do it for the cells of a specific column, I don't know if it will be possible.

Thank you for your answers.

FuryFox
  • 1
  • 1
  • I kind of like the idea of a "row header" as mentioned [here](https://community.oracle.com/tech/developers/discussion/1380000/how-to-disable-jtable-column-dragging-only-single-column) – MadProgrammer Mar 29 '22 at 23:01
  • Or even https://stackoverflow.com/questions/2104055/disable-a-single-column-dragging-in-jtable – MadProgrammer Mar 29 '22 at 23:04

1 Answers1

0

When an object is dropped on the JTable, you can check the drop location and decide if the operation is accepted or not.

For example, to accept drops on the first column only:

JTable table = new JTable();
table.setTransferHandler(new TransferHandler() {
    @Override
    public boolean canImport(TransferSupport support) {
        if (support.isDrop()) {
            JTable.DropLocation dropLocation = (JTable.DropLocation) support.getDropLocation();
            if (dropLocation.getColumn() != 0) {
                return false;
            }
        }

        return super.canImport(support);
    }

    @Override
    public boolean importData(TransferSupport support) {
        if (support.isDrop() && canImport(support)) {
            // handle the dropped object
            ...
            return true;
        }

        return super.importData(support);
    }
});
Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76