• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add JScrollPane to JTextArea?  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have two JTextAreas, left and right (leftA and rightA). I have tried to add JScrollPane (vertical) to left text area but I hadn't succeed with it. I have tried to do that with two methods (other is commented away) but either of them is not working. Please, can somebody help me with that?

 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

Create the text area
create the scroll pane with the text area as parm

Your commented code line 12 will do the trick for "leftA"
 
Matt Lehto
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

Create the text area
create the scroll pane with the text area as parm

Your commented code line 12 will do the trick for "leftA"


For some reasons, commented lines are not working. Without any scroll panes situation is like this (two text areas, leftA and rightA):


But if I take to use lines 12-14 and comment away lines 16-18, results are:


Why?
 
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JTextArea should essentially always be inside a JScrollPane.

So you should be doing something like lines 12-14 for both leftA and rightA. (If you want to set VERTICAL_SCROLLBAR_ALWAYS for the left scroll pane and not for the right, that's fine.)

Line 17 doesn't make sense—don't do that. (JTextArea does inherit from java.awt.Container, but you should pretend that it doesn't.)

I see the two images you posted, but I can't tell what you're asking. What exactly do you want to know?
 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And thus, as others have pointed out, you do not add a JScrollPane to JTextArea.

Instead, you do it the other way around - you add a JTextArea to a JScrollPane.
So even though the method is "add()", you should think of GUI containers as containing other items.

The scroll bars of the scroll pane are completely outside the text area, so you can see that the scroll pane it the thing that contains the text area.
The function "x.add(y)" should be interpreted as "place y as an item contained inside x".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!