Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add scroll into text area

How can I add the scroll bar to my text area. I have tried with this code but it's not working.

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);
middlePanel.add(display);
like image 912
Ravi Avatar asked Apr 16 '12 15:04

Ravi


3 Answers

After adding JTextArea into JScrollPane here:

scroll = new JScrollPane(display); 

You don't need to add it again into other container like you do:

middlePanel.add(display); 

Just remove that last line of code and it will work fine. Like this:

    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); 

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:

new JScrollPane( myComponent )  

or set view like this:

JScrollPane pane = new JScrollPane (); pane.getViewport ().setView ( myComponent ); 

Additional:

Here is fully working example since you still did not get it working:

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 ); } 

And here is what you get: enter image description here

like image 124
Mikle Garin Avatar answered Sep 19 '22 22:09

Mikle Garin


My naive assumption was that the size of scroll pane will be determined automatically...

The only solution that actually worked for me was explicitly seeting bounds of JScrollPane:

import javax.swing.*;  public class MyFrame extends JFrame {      public MyFrame()     {         setBounds(100, 100, 491, 310);         getContentPane().setLayout(null);          JTextArea textField = new JTextArea();         textField.setEditable(false);          String str = "";         for (int i = 0; i < 50; ++i)             str += "Some text\n";         textField.setText(str);          JScrollPane scroll = new JScrollPane(textField);         scroll.setBounds(10, 11, 455, 249);                     // <-- THIS          getContentPane().add(scroll);         setLocationRelativeTo ( null );     } } 

Maybe it will help some future visitors :)

like image 39
LihO Avatar answered Sep 22 '22 22:09

LihO


Try adding these two lines to your code. I hope it will work. It worked for me :)

display.setLineWrap(true);
display.setWrapStyleWord(true);

Picture of output is shown below

enter image description here

like image 44
farhangdon Avatar answered Sep 22 '22 22:09

farhangdon