简体   繁体   English

从单个列拖放行(不是整行)

[英]drag and drop rows from a single column (not whole row)

based on this post I have implemented a handy feature to drag and drop rows in a JTable to re-order them. 基于这篇文章,我已经实现了一个方便的功能来拖放JTable中的行来重新排序它们。 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. 我之前已经看过这个概念(不是在java中),但现在找不到合适的例子。

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. 编辑:@ Kleopatra的答案是好的,但我的问题的一部分是我的其他列包含comoponents,如按钮,由于拖动处理程序,不再显示其“按下”状态。 i'm hoping to find a solution which restricts the drag mouse handler so that it impacts the first column. 我希望找到一个限制拖动鼠标处理程序的解决方案,以便它影响第一列。

To start dnd only if the mouse drag is initiated in the (fi) the first column of the table, implement the exportAsDrag method of the custom TransferHandler to return NONE elsewhere, something like: 要仅在表的第一列(fi)中启动鼠标拖动时启动dnd,请实现自定义TransferHandler的exportAsDrag方法以将NONE返回到其他位置,例如:

@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM