如何在Java Swing中拖放期间启用工具提示显示

时间:2010-06-29 00:23:32

标签: java swing

如何在执行拖放操作时显示工具提示。 似乎禁用或不触发拖放操作期间的工具提示显示。 我想使用工具提示向用户表明丢弃的原因。

2 个答案:

答案 0 :(得分:3)

对我有用的解决方案是在TransferHandler中手动创建工具提示。这是我添加的代码:

public class TableTransferHandler extends TransferHandler {
    private Popup tipWindow;
    private int tipCol;
    private int tipRow;

    public boolean canImport(TransferHandler.TransferSupport support) {
        ....
        updateDropDeniedTooltip(support, deniedReason)
    }

    private void hideDropDeniedTooltip() {
        if (tipWindow != null) {
            tipWindow.hide();
            tipWindow = null;
        }
    }

    private void updateDropDeniedTooltip(TransferHandler.TransferSupport support, String deniedReason) {
        if (deniedReason != null) {
            JTable.DropLocation dropLocation = (JTable.DropLocation)support.getDropLocation();
            JTable jtable = (JTable)support.getComponent();
            if (tipWindow != null) {
                if (tipRow != dropLocation.getRow() || tipCol != dropLocation.getColumn()) {
                    hideDropDeniedTooltip();
                }
            }
            if (tipWindow == null) {
                tipRow = dropLocation.getRow();
                tipCol = dropLocation.getColumn();
                JToolTip tip = jtable.createToolTip();
                tip.setTipText(result.getReason());
                PopupFactory popupFactory = PopupFactory.getSharedInstance();
                Rectangle cellRect = jtable.getCellRect(tipRow, tipCol, true);
                Point location = jtable.getLocationOnScreen();
                location.x += cellRect.x;
                location.y += cellRect.y;
                tipWindow = popupFactory.getPopup(jtable, tip, location.x, location.y);
                tipWindow.show();
            }
        }
        else {
            hideDropDeniedTooltip();
        }
    }
 }

答案 1 :(得分:1)

您可以利用Drop Location Rendering来达到类似的效果。