Note that there are some explanatory texts on larger screens.

plurals
  1. POJava / Swing : JTextArea in a JScrollPane, how to prevent auto-scroll?
    primarykey
    data
    text
    <p>here's a runnable piece of code that shows what my "problem" is.</p> <p>I've got a <code>JTextArea</code> wrapped in a <code>JScrollPane</code>. When I change the text of the <code>JTextArea</code>, the <code>JScrollPane</code> scrolls automatically to the end of the text and I don't want that.</p> <p>Here are my requirements:</p> <ul> <li>the application <em>should not</em> scroll vertically automatically but...</li> <li>the user <em>should</em> be able to scroll vertically</li> <li>the user <em>should not</em> be able to scroll horizontally</li> <li>the application <em>should never</em> scroll horizontally</li> <li>the JTextArea must not be editable</li> </ul> <p>(so even if there's more text than what can fit horizontally, neither the application nor the user should be able to scroll horizontally. While vertically, only the user should be able to scroll.)</p> <p>I don't know how to "fix" this: should this be fixed using <code>JTextArea</code> or <code>JScrollPane</code> methods? </p> <p>Note that AFAICT this is <em>not</em> a duplicate at all of: <a href="https://stackoverflow.com/questions/1377887">JTextPane prevents scrolling in the parent JScrollPane</a></p> <p>Here's the kinda funny example, every 200 ms it puts new text in the <code>JTextArea</code> and you can see the <code>JScrollPane</code> always scrolling automatically to the end of the text.</p> <pre><code>import javax.swing.*; import java.awt.*; import java.util.Random; public final class TextInScrollPane extends JFrame { private static final Random r = new Random( 42 ); public static void main( final String[] args ) { final JFrame f = new JFrame(); f.setDefaultCloseOperation( EXIT_ON_CLOSE ); f.setLayout(new BorderLayout()); final JTextArea jta = new JTextArea( "Some text", 30, 30 ); jta.setEditable( false ); // This must not be editable final JScrollPane jsp = new JScrollPane( jta ); jsp.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ); jsp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ); f.add( jsp, BorderLayout.CENTER ); f.pack(); f.setLocationRelativeTo( null ); f.setVisible(true); final Thread t = new Thread( new Runnable() { public void run() { while ( true ) { try {Thread.sleep( 200 );} catch ( InterruptedException e ) {} final StringBuilder sb = new StringBuilder(); for (int i = 0; i &lt; 50 + r.nextInt( 75 ); i++) { for (int j = 0; j &lt; r.nextInt(120); j++) { sb.append( (char) 'a' + r.nextInt(26) ); } sb.append( '\n' ); } SwingUtilities.invokeLater( new Runnable() { public void run() { jta.setText( sb.toString() ); } } ); } } }); t.start(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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