0

我有一个可以删除组件的 JTable。其中一些组件只能作为表中的插入删除(因此不能在已经存在的单元格上)。

我的问题是如何禁止放置这些组件的单元格。我试过类似的东西:

JTable table = new JTable();
table.setDropMode(DropMode.ON_OR_INSERT_COLS);
table.setTransferHandler(new ExampleTransferHandler());
boolean onlyColumnInsert = true;

private class ExampleTransferHandler extends TransferHandler{
    public boolean canImport(TransferSupport support){
        if(onlyColumnInsert){
            return table.getDropLocation().isInsertColumn();
        }else{
            return true;
        }
    }
}

但这不起作用,因为 isInsertColumn() 仅在放置完成后设置。有没有其他方法可以检测删除是否会导致从 TransferHandler 中的 canImport() 方法插入列?

谢谢!

4

1 回答 1

0

通过将 TransferSupport 携带的 DropLocation 转换为 JTable.DropLocation,我找到了解决此问题的方法:

private class ExampleTransferHandler extends TransferHandler{
  public boolean canImport(TransferSupport support){
    if(onlyColumnInsert){
        return ((JTable.DropLocation)support.getDropLocation()).isInsertColumn();
    }else{
        return true;
    }
  }
}
于 2012-07-05T09:49:18.050 回答