Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Edited to add full solution</p> <p>You have to add a JScrollPane first. Then, if you don't want the scrollbars to be visible, but you want the text area to autoscroll, set </p> <pre><code>scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); </code></pre> <p>on the scrollpane. This will hide the scrollbars, but provide you with autoscrolling.</p> <p>Here's how you'd implement auto scrolling with scroll pane, and automatic resize upto a given maximum. </p> <pre><code>import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; public class SPTest extends JFrame { private static final long serialVersionUID = 1L; private JEditorPane editor; private JScrollPane scrollPane; private JPanel topPanel; private JLabel labelTop; public SPTest() { super("Editor test"); initComponents(); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } private void initComponents() { editor = new JEditorPane("text/plain", null); scrollPane = new JScrollPane(editor); topPanel = new JPanel(); labelTop = new JLabel("main contents here"); topPanel.add(labelTop); setSize(600, 400); editor.setMinimumSize(new Dimension(100, 30)); editor.setPreferredSize(new Dimension(100, 60)); scrollPane.setPreferredSize(new Dimension(600, 60)); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); scrollPane.setMinimumSize(new Dimension(100, 30)); final int MAX_HEIGHT_RSZ = 120; editor.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent e) { int height = Math.min(editor.getPreferredSize().height, MAX_HEIGHT_RSZ); Dimension preferredSize = scrollPane.getPreferredSize(); preferredSize.height = height; scrollPane.setPreferredSize(preferredSize); SPTest.this.validate(); } }); setLayout(new BorderLayout()); add(topPanel, BorderLayout.NORTH); add(scrollPane, BorderLayout.SOUTH); } public static void main(String[] args) { new SPTest(); } } </code></pre> <p>You could resize You can use this JScrollPane instead of the JPanel as the container for the editor.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload