Solution is, no matter what textComponent
is (whether JTextArea
or JEditorPane
):
textComponent.setFont(new JTextArea().getFont().deriveFont(11.5f));
This forces JEditorPane
to use the same font as JTextArea
. Additionally, replace spaces with html entities and newlines with <br>
:
sampleText=sampleText.replaceAll("\n","<br>").replaceAll("\\s"," ");