通过点击来区分拖放

时间:2018-05-18 15:56:55

标签: java swing

我正在尝试实现一种功能,我拖放以重新排序我的JList,同时如果单击其中一个对象,则会显示一个弹出菜单。当我开始拖动并出现弹出窗口时出现问题。如何才能在不拖动时显示此弹出窗口?

这是我的tranferhandlerclass:

public class CustomTransferHandler extends TransferHandler {

    private DefaultListModel<String> dataUser;

    private int index;
    private boolean beforeIndex = false;
    private JList<String> userColumn;
    private JScrollPane jScrollPane;

    public CustomTransferHandler(JList<String> userColumn, DefaultListModel<String> dataUser, JScrollPane jScrollPane){
        this.userColumn = userColumn;
        this.jScrollPane = jScrollPane;
        this.dataUser = dataUser;
    }

    @Override
    public int getSourceActions(JComponent comp) {
        return COPY_OR_MOVE;
    }

    @Override
    public Transferable createTransferable(JComponent comp) {
        index = userColumn.getSelectedIndex();
        return new StringSelection((String) userColumn.getSelectedValue());
    }

    @Override
    public void exportDone(JComponent comp, Transferable trans, int action) {
        if (action == MOVE) {
            if (beforeIndex) {
                dataUser.remove(index + 1);
            } else {
                dataUser.remove(index);
            }
            jScrollPane.updateUI();
        }
    }

    @Override
    public boolean canImport(TransferHandler.TransferSupport support) {
        // Data =? String
        return support.isDataFlavorSupported(DataFlavor.stringFlavor);
    }

    @Override
    public boolean importData(TransferHandler.TransferSupport support) {
        try {
            // Data to String
            String s = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            dataUser.add(dl.getIndex(), s);
            beforeIndex = dl.getIndex() < index ? true : false;
            return true;
        } catch (UnsupportedFlavorException | IOException e) {
        }
        return false;
    }

}

点击它是一个带有actionEventController的简单控制器。

0 个答案:

没有答案