0

I'm trying to create a scroll bar for my text area. However, the scroll bar isn't appearing. Can anyone give me any tips. This is the from the method which creates the panel where the scroll bar will be.

displayCD = new JPanel();
displayCD.setSize(new Dimension(500, 500));

jta = new JTextArea();
jta.setMaximumSize(new Dimension(500, 500));
scrollPane = new JScrollPane();
scrollPane.getViewport().add(jta);

displayCD.add(scrollPane);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Steven
  • 93
  • 1
  • 11

3 Answers3

0

You need to provide the textArea to the constructor of the scrollpane. Please check this link, for more information: http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html.

JScrollPane scrollPane = new JScrollPane(textArea);

Iootu
  • 344
  • 1
  • 6
  • I am not the downvoter but it's not necessary to use constructor only. – StanislavL Oct 24 '14 at 11:02
  • I was providing a quick answer and then editing for the sake of completeness. Taking a look at the other answers the only thing they add is the use of the scrollBarPolicy... – Iootu Oct 24 '14 at 11:25
  • 1
    That the OP here is setting sizes at all seems to be the root of the problem. Most of these answers are, therefore, noise. – Andrew Thompson Oct 24 '14 at 11:45
  • OK, I acknowledge that I wasn't aware of the issues regarding the sizes, thanks for clarifying it. – Iootu Oct 24 '14 at 12:02
0

Check out How to Use Scroll Panes. Here follows their example:

//In a container that uses a BorderLayout:
textArea = new JTextArea(5, 30);
...
JScrollPane scrollPane = new JScrollPane(textArea);
...
setPreferredSize(new Dimension(450, 110));
...
add(scrollPane, BorderLayout.CENTER);

Try to pass the JTextArea in the JScrollPane's constructor, and, most of all, try to give it meaningful size hints (rows and columns).
Applying this to your specific case:

jta = new JTextArea(5, 30);
scrollPane = new JScrollPane(jta);

displayCD = new JPanel();    
displayCD.add(scrollPane);

The scroll bar should appear, if your text contents exceed the default dimensions. To always show the bars, try the setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy methods, or other JScrollPane constructors.

Regarding the size of the text area:

Unless you explicitly set a scroll pane's preferred size, the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners). The largest factor, and the one most programmers care about, is the size of the viewport used to display the client.

afsantos
  • 5,178
  • 4
  • 30
  • 54
  • 3
    *"Applying this to your specific case:"* It would have been better off retaining things like `textArea = new JTextArea(5, 30);` over `jta.setMaximumSize(new Dimension(500, 500));`. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Oct 24 '14 at 11:42
  • @AndrewThompson Indeed. I had already seen that question before, hence the comments I added to the code. However it's more valuable to the answer to have the right code instead of the comments. – afsantos Oct 24 '14 at 14:41
0

see this example. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS property.

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main {
  public static void main(String[] argv) throws Exception {
    JTextArea textArea = new JTextArea();
    JScrollPane pane = new JScrollPane(textArea);
    pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

  }
}

Reference: http://www.java2s.com/Code/JavaAPI/javax.swing/JScrollPaneVERTICALSCROLLBARALWAYS.htm

Kumar
  • 3,782
  • 4
  • 39
  • 87