By Alvin Alexander. Last updated: June 6, 2016
I use this following Scala source code to create a Java JTextArea
component, set its font, make it readonly, set its margin (insets), then put the textarea into a JScrollPane
:
// text area val textArea = new JTextArea(textAreaRows, textAreaColumns) textArea.setFont(new Font("Helvetica Neue", Font.PLAIN, 20)) textArea.setEditable(false) textArea.setMargin(new Insets(12, 12, 12, 12)) textArea.setText(textToDisplay) // scrollpane val scrollPane = new JScrollPane(textArea) scrollPane.setPreferredSize(windowSize) scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS)
While the code is written in Scala, it converts easily to Java, as you can see. If you wanted to see any combination of setting a JTextArea
in a JScrollPane
, setting the textarea font or margins, setting the scrollpane size and/or controlling the scrollbars, I hope this is helpful.