gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:55:05 25 4
gpt4 key购买 nike

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

最佳答案

对我有用的解决方案是在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com