java - 如何在 Java Swing 中的拖放过程中启用工具提示显示

标签 java swing

如何在执行拖放时显示工具提示。 似乎禁用或未触发拖放操作期间的工具提示显示。 我想使用工具提示向用户指示为什么拒绝放置。

最佳答案

对我有用的解决方案是在 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();
        }
    }
 }

关于java - 如何在 Java Swing 中的拖放过程中启用工具提示显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3137065/

相关文章:

java - 限制 JTextField 中的输入长度不起作用

java - 添加计时器并显示标签文本

java - 使用 mvc 时将在不同类上创建的 jpanel 添加到主视图页面

java - 如何从 Controller 获取 main-servlet.xml 中定义的 bean

Java赋值

java - Jlabel 额外可点击空间

java - 希望 JDialog 为 "modal"但不阻止其他窗口

java - 来自 URL 的 GWT RPC 调用

java - 从菜单保存和打开对象

java - 从复选框获取值