Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After adding JTextArea into JScrollPane here:</p> <pre><code>scroll = new JScrollPane(display); </code></pre> <p>You don't need to add it again into other container like you do:</p> <pre><code>middlePanel.add(display); </code></pre> <p>Just remove that last line of code and it will work fine. Like this:</p> <pre><code> middlePanel=new JPanel(); middlePanel.setBorder(new TitledBorder(new EtchedBorder(), "Display Area")); // create the middle panel components display = new JTextArea(16, 58); display.setEditable(false); // set textArea non-editable scroll = new JScrollPane(display); scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //Add Textarea in to middle panel middlePanel.add(scroll); </code></pre> <p>JScrollPane is just another container that places scrollbars around your component when its needed and also has its own layout. All you need to do when you want to wrap anything into a scroll just pass it into JScrollPane constructor:</p> <pre><code>new JScrollPane( myComponent ) </code></pre> <p>or set view like this:</p> <pre><code>JScrollPane pane = new JScrollPane (); pane.getViewport ().setView ( myComponent ); </code></pre> <p><strong>Additional:</strong></p> <p>Here is fully working example since you still did not get it working:</p> <pre><code>public static void main ( String[] args ) { JPanel middlePanel = new JPanel (); middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) ); // create the middle panel components JTextArea display = new JTextArea ( 16, 58 ); display.setEditable ( false ); // set textArea non-editable JScrollPane scroll = new JScrollPane ( display ); scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS ); //Add Textarea in to middle panel middlePanel.add ( scroll ); // My code JFrame frame = new JFrame (); frame.add ( middlePanel ); frame.pack (); frame.setLocationRelativeTo ( null ); frame.setVisible ( true ); } </code></pre> <p>And here is what you get: <img src="https://i.stack.imgur.com/sOqmC.png" alt="enter image description here"></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