【问题标题】:JTable: howto detect column insert before drop completes?JTable:如何在删除完成之前检测列插入?
【发布时间】:2012-06-13 09:26:43
【问题描述】:

我有一个可以删除组件的 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() 方法检测 drop 是否会导致插入列?

谢谢!

【问题讨论】:

  • 请出示证明问题的 SSCCE。

标签: java swing drag-and-drop jtable


【解决方案1】:

我找到了解决此问题的方法,方法是将 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;
    }
  }
}

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 2020-11-24
    • 2016-07-12
    • 1970-01-01
    • 2016-07-18
    • 2012-05-05
    • 1970-01-01
    • 2017-02-02
    • 2013-01-03
    相关资源
    最近更新 更多