JEditorPane (@Tooltip Html html, HintHint hintHint, @Nullable JLayeredPane layeredPane, boolean limitWidthToScreen) { @NonNls String text = HintUtil.prepareHintText(html, hintHint); boolean[] prefSizeWasComputed = {false}; JEditorPane pane = !limitWidthToScreen ? new JEditorPane() : new JEditorPane() { private Dimension prefSize = null; @Override public Dimension getPreferredSize() { if (!prefSizeWasComputed[0] && hintHint.isAwtTooltip()) { JLayeredPane lp = layeredPane; if (lp == null) { JRootPane rootPane = UIUtil.getRootPane(this); if (rootPane != null && rootPane.getSize().width > 0) { lp = rootPane.getLayeredPane(); } } Dimension size; if (lp != null) { AppUIUtil.targetToDevice(this, lp); size = lp.getSize(); prefSizeWasComputed[0] = true; } else { size = ScreenUtil.getScreenRectangle(0, 0).getSize(); } int fitWidth = (int)(size.width * 0.8); Dimension prefSizeOriginal = super.getPreferredSize(); if (prefSizeOriginal.width > fitWidth) { setSize(new Dimension(fitWidth, Integer.MAX_VALUE)); Dimension fixedWidthSize = super.getPreferredSize(); Dimension minSize = super.getMinimumSize(); prefSize = new Dimension(Math.max(fitWidth, minSize.width), fixedWidthSize.height); } else { prefSize = new Dimension(prefSizeOriginal); } } Dimension s = prefSize != null ? new Dimension(prefSize) : super.getPreferredSize(); Border b = getBorder(); if (b != null) { JBInsets.addTo(s, b.getBorderInsets(this)); } return s; } @Override public void setPreferredSize(Dimension preferredSize) { super.setPreferredSize(preferredSize); prefSize = preferredSize; } }; HTMLEditorKit kit = new HTMLEditorKitBuilder() .withViewFactoryExtensions((elem, view) -> { AttributeSet attrs = elem.getAttributes(); if (attrs.getAttribute(AbstractDocument.ElementNameAttribute) == null && attrs.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.HR) { try { Field field = view.getClass().getDeclaredField("size"); field.setAccessible(true); field.set(view, JBUIScale.scale(1)); } catch (Exception ignored) { } } return view; }) .build(); String editorFontName = EditorColorsManager.getInstance().getGlobalScheme().getEditorFontName(); if (editorFontName != null) { String style = "font-family:\"" + StringUtil.escapeQuotes(editorFontName) + "\";font-size:95%;"; kit.getStyleSheet().addRule("pre {" + style + "}"); text = text.replace("<code>", "<code style='" + style + "'>"); } pane.setEditorKit(kit); pane.setText(text); pane.setCaretPosition(0); pane.setEditable(false); if (hintHint.isOwnBorderAllowed()) { setBorder(pane); setColors(pane); } else { pane.setBorder(null); } if (!hintHint.isAwtTooltip()) { prefSizeWasComputed[0] = true; } pane.setOpaque(hintHint.isOpaqueAllowed()); pane.setBackground(hintHint.getTextBackground()); if (!limitWidthToScreen) { AppUIUtil.targetToDevice(pane, layeredPane); } return pane; }