void (@NotNull Editor editor, @NotNull JComponent editorComponent, @NotNull Point p, int width, int height, int heightLimit) { PointerInfo pointerInfo = MouseInfo.getPointerInfo(); if (pointerInfo == null) return; Point mouse = pointerInfo.getLocation(); SwingUtilities.convertPointFromScreen(mouse, editorComponent); Rectangle tooltipRect = new Rectangle(p, new Dimension(width, height)); // should show at least one line apart tooltipRect.setBounds(tooltipRect.x, tooltipRect.y - editor.getLineHeight(), width, height + 2 * editor.getLineHeight()); if (tooltipRect.contains(mouse)) { if (mouse.y + height + editor.getLineHeight() > heightLimit && mouse.y - height - editor.getLineHeight() > 0) { p.y = mouse.y - height - editor.getLineHeight(); } else { p.y = mouse.y + editor.getLineHeight(); } } }