JPanel (@NotNull HintHint hintHint, @NotNull JScrollPane pane, @NotNull JEditorPane editorPane, boolean highlightActions, boolean hasSeparators) { int leftBorder = 10; int rightBorder = 12; final class MyPanel extends JPanel implements WidthBasedLayout { private MyPanel() { super(new GridBagLayout()); } @Override public int getPreferredWidth() { return getPreferredSize().width; } @Override public int getPreferredHeight(int width) { Dimension size = editorPane.getSize(); int editorPaneInsets = leftBorder + rightBorder + getSideComponentWidth(); editorPane.setSize(width - editorPaneInsets, Math.max(1, size.height)); int height; try { height = getPreferredSize().height; if (width - editorPaneInsets < editorPane.getMinimumSize().width) { JScrollBar scrollBar = pane.getHorizontalScrollBar(); if (scrollBar != null) height += scrollBar.getPreferredSize().height; } } finally { editorPane.setSize(size); } return height; } @Override public AccessibleContext getAccessibleContext() { return new AccessibleContextDelegate(editorPane.getAccessibleContext()) { @Override protected Container getDelegateParent() { return getParent(); } }; } private int getSideComponentWidth() { GridBagLayout layout = (GridBagLayout)getLayout(); Component sideComponent = null; GridBagConstraints sideComponentConstraints = null; boolean unsupportedLayout = false; for (Component component : getComponents()) { GridBagConstraints c = layout.getConstraints(component); if (c.gridx > 0) { if (sideComponent == null && c.gridy == 0) { sideComponent = component; sideComponentConstraints = c; } else { unsupportedLayout = true; } } } if (unsupportedLayout) { Logger.getInstance(LineTooltipRenderer.class).error("Unsupported tooltip layout"); } if (sideComponent == null) { return 0; } else { Insets insets = sideComponentConstraints.insets; return sideComponent.getPreferredSize().width + (insets == null ? 0 : insets.left + insets.right); } } } JPanel grid = new MyPanel(); GridBag bag = new GridBag() .anchor(GridBagConstraints.CENTER) //weight is required for correct working scrollpane inside gridbaglayout .weightx(1.0) .weighty(1.0) .fillCell(); pane.setBorder(JBUI.Borders.empty(10, leftBorder, (highlightActions ? 10 : (hasSeparators ? 8 : 3)), rightBorder)); grid.add(pane, bag); grid.setBackground(hintHint.getTextBackground()); grid.setBorder(JBUI.Borders.empty()); grid.setOpaque(hintHint.isOpaqueAllowed()); return grid; }