JComponent (@NotNull JComponent comp, int columns, int maxLines, int lines) { float fontSize = comp.getFont().getSize2D(); Dimension maxDim = new Dimension((int)(fontSize * columns), (int)(fontSize * maxLines)); Dimension prefDim = comp.getPreferredSize(); if (prefDim.width <= maxDim.width && prefDim.height <= maxDim.height) return comp; JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(comp); scrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); int barWidth = UIUtil.getScrollBarWidth(); Dimension preferredSize = new Dimension(Math.min(prefDim.width, maxDim.width) + barWidth, Math.min(prefDim.height, maxDim.height) + barWidth); if (prefDim.width > maxDim.width) { //Too wide single-line message should be wrapped preferredSize.height = Math.max(preferredSize.height, (int)(lines * fontSize) + barWidth); } scrollPane.setPreferredSize(preferredSize); return scrollPane; }