How to enable tooltip display during drag-n-drop in Java Swing
How can I show tooltips while performing drag-n-drop. It seems that tooltip display during drag-n-drop operation is disabled or not triggered. I want to use the tooltip to indicate to the user why t开发者_如何学运维he drop is being denied.
The solution that worked for me was to create the tool tip manually inside the TransferHandler. Here is the code I added:
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();
}
}
}
You may be able to take advantage of Drop Location Rendering to achieve a similar effect.
精彩评论