Dimension (int charCount, @NotNull JComponent comp) { Dimension size = comp.getPreferredSize(); FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont()); size.width = fontMetrics.charWidth('a') * charCount; return size; }