Skip to content

Commit

Permalink
improve tooltip positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
dpolivaev committed Apr 13, 2019
1 parent aff7056 commit 0359aa1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Dimension getPreferredSize() {
@Override
public void layout() {
final Component renderer = getComponent(0);
renderer.setSize(getPreferredSize());
renderer.setSize(getSize());
super.layout();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@


import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Expand Down Expand Up @@ -157,18 +159,25 @@ private void showTipWindow() {
return;
tip = insideComponent.createToolTip();
tip.setTipText(toolTipText);
final JComponent nearComponent = insideComponent;
focusOwnerRef = new WeakReference<Component>(focusOwner);
tipPopup = new JPopupMenu();
tipPopup.setLayout(new GridLayout(1, 1));
tipPopup.add(tip);
mouseInsideTooltipListener = new MouseInsideListener(tipPopup);
final Component placedComponent = tipPopup;
Point location = UITools.findBestLocation(placedComponent, nearComponent);
SwingUtilities.convertPointFromScreen(location, nearComponent);
tipPopup.show(nearComponent, location.x, location.y);
focusOwner.requestFocusInWindow();
exitTimer.start();
final Rectangle desktopBounds = UITools.getAvailableScreenBounds(insideComponent);
final Dimension popupPreferredSize = tipPopup.getPreferredSize();
final Point desiredLocation = new Point(0, insideComponent.getHeight());
SwingUtilities.convertPointToScreen(desiredLocation, insideComponent);
int popupAllowedHeight = desktopBounds.y + desktopBounds.height - desiredLocation.y;
if(popupAllowedHeight > 0) {
Dimension popupSize = new Dimension(
popupPreferredSize.width,
Math.min(popupAllowedHeight, popupPreferredSize.height));
tipPopup.setPreferredSize(popupSize);
tipPopup.show(insideComponent, 0, insideComponent.getHeight());
focusOwner.requestFocusInWindow();
exitTimer.start();
}
}

private void hideTipWindow() {
Expand Down

0 comments on commit 0359aa1

Please sign in to comment.