aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding ScrollBar to JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding ScrollBar to JTextArea" Watch "Adding ScrollBar to JTextArea" New topic
Author

Adding ScrollBar to JTextArea

Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
I would like to add vertical ScrollBar to my Text Area.

TextArea is in Panel:


Does anyone has some ideas or suggestions? Any help is welcome!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Add the scroller to the panel, not the text area. (See the comment at line 10? Don't do that.)
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Ok, I changed to:



But still I cannot see plainTextArea, it is not visible ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Why do you add a JScrollBar to the JScrollPane? It takes care of the scroll bars itself.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
I didn't know that..Ok, but still I don't know why this code is not working ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Is BorderLayout.EAST compatible with the actual layout manager being used in your JPanel?

Edit: looking again at your code I see you've chosen to use a null layout for your JPanel. Why did you do that? Anyway BorderLayout.EAST isn't going to do anything in a null layout.

(Although I'm confused between groupBoxDecryption, whose declaration I see but I don't see being used, and groupBoxEncryption, which I see being used but whose declaration I don't see.)
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
I'm sorry, it is my mistake, declaration should be for groupBoxEncryption (I have both in my code, so I made mistake, since I was coping fast..).

I use null layout, since I am Java beginner, and I didn't have time at the moment to explore all different layouts, so I choose null, because I can put controls like I would like.

Ok, is it possible somehow to add here vertical scroll bar, in null layout ? And how to do it ?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
I didn't have time at the moment to explore all different layouts


So instead you waste our time because you think your time is more valuable than ours?

It was suggested 2 weeks ago that you don't use null layouts and instead learn how to use layout managers. Yet you ignore the advice and now are taking more of our time because you don't understand whats happening.

The basic code is simple:

JTextArea textArea = new JTextArea(4, 20);
JScrollPane scrollPane = new JScrollPane( textArea );
somePanel.add( scrollPane );


If you need more help post your SSCCE that uses a layout manger.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Nicol Green wrote:I use null layout, since I am Java beginner, and I didn't have time at the moment to explore all different layouts, so I choose null, because I can put controls like I would like.

Ok, is it possible somehow to add here vertical scroll bar, in null layout ? And how to do it ?


Then presumably, since you chose the null layout so you could put the components (they are called "controls" in Visual Basic if I recall right) where you wanted, you should be able to put that scroll pane where you wanted it. But you didn't do that.
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Actually I didn't ignored Your advice, just I haven't found layout which would meet my requirements, so instead I used null layout since I have a deadline for finishing this project.

I am sorry to waist Your time, I just thought this is a friendly place for greenhorns.

I also see that basic code is simple, but I just wanted to adopt it to my code, because of that I asked for assistance.

As SSCCE concerned I am really trying to provide it, but I have about 1000 line of code, and mistakes are possible when copying, once more, sorry for that.

Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Paul Clapham wrote:
Nicol Green wrote:I use null layout, since I am Java beginner, and I didn't have time at the moment to explore all different layouts, so I choose null, because I can put controls like I would like.

Ok, is it possible somehow to add here vertical scroll bar, in null layout ? And how to do it ?


Then presumably, since you chose the null layout so you could put the components (they are called "controls" in Visual Basic if I recall right) where you wanted, you should be able to put that scroll pane where you wanted it. But you didn't do that.


Ok, this is not an issue, lets suppose I can locate components where I want in my panel, but I don't now how to add ScrollBar to my TextArea, which is placed in the panel.


All this question is about that: Is it possible to do it in system like I projected it, above ?
If you know some better solution, I will take a chance!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Nicol Green wrote:Ok, this is not an issue, lets suppose I can locate components where I want in my panel, but I don't now how to add ScrollBar to my TextArea, which is placed in the panel.


Well, you don't. You put your text area into a JScrollPane and add that to the panel. But we already went through that, didn't we? You had some code where you did that.

Edit: sorry, I see it was Rob Camick who posted that code. You didn't have that in your original code. But that's what you should do.

And then, since it's the JScrollPane you are adding to the panel, it's the JScrollPane which you have to provide a location and size for. Although if you had used one of the regular layout managers, you wouldn't have to be dealing with that issue.
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Ok, thanks! I tried that, as you said, but I don't know why it is not visible.

Probably some mistake in changing, will go trough this again..

When it start to work correctly I will provide code here.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Nicol Green wrote:I just thought this is a friendly place for greenhorns.

It usually is, but some people don't appreciate it if you ignore their advice, then post a problem that could have been prevented by following that advice.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Rob Prime wrote:
Nicol Green wrote:I just thought this is a friendly place for greenhorns.

It usually is, but some people don't appreciate it if you ignore their advice, then post a problem that could have been prevented by following that advice.


Add me to that list. And to further the point, there have been several pieces of advice in this posting, yet we still don't have a SSCCE (which has also been suggested in more than one of her postings) to see how all this advice has been applied. So is the problem bad advice or bad implementation of the advice?

As SSCCE concerned I am really trying to provide it, but I have about 1000 line of code, and mistakes are possible when copying, once more, sorry for that.


That is not how to create a SSCCE. The point of a SSCCE is to start over and start with something really simple to make sure you understand the concept. It you can get it working then great you compare the SSCCE code with your real code to see whats different. If the SSCCE doesn't work thats also ok, because now you have something simple to post in the forum. This is a way to force you to focus on a smaller problem to learn a concept, rather than trying to understand a concept by looking at 1000 lines of code.
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Rob Camick wrote:
Rob Prime wrote:
Nicol Green wrote:I just thought this is a friendly place for greenhorns.

It usually is, but some people don't appreciate it if you ignore their advice, then post a problem that could have been prevented by following that advice.


Add me to that list. And to further the point, there have been several pieces of advice in this posting, yet we still don't have a SSCCE (which has also been suggested in more than one of her postings) to see how all this advice has been applied. So is the problem bad advice or bad implementation of the advice?


Ok, I see what You mean. Certainly it is my bad implementation of Your advice. Thank You all for the assistance.

I tried different ways, but It seems that null layout is the worst (you had point..) of all
So, no way to do it like that, but for other beginners, I think, this will be an useful topic, when with runnable code, even
better.

Now I am researching others, and hopefully will make that scroll eventually.
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Here it is with null layout, still:



It wooorks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

groupBoxEncryption does not have a null layout; JPanels use a FlowLayout by default.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Here it is with null layout,


As Rob pointed out you are using the FlowLayout by default on your panel, which mean the setBounds(...) code is completely unnecessary since the layout manager will reset the size and location of the scrollpane based on the rules of the layout manager.

Also, setVisible(..) statement is unnecessaray as all Swing components (except for JFrame and JDialog) are visible by default. Remove those two lines and the code looks exactly like what I suggested in my first posting.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Rob Camick wrote:all Swing components (except for JFrame and JDialog) are visible by default.

And JInternalFrame (for completeness).
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Rob Prime wrote:
Rob Camick wrote:all Swing components (except for JFrame and JDialog) are visible by default.

And JInternalFrame (for completeness).


Darn, I knew I should have just said "top level containers" ;)
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
As Rob pointed out you are using the FlowLayout by default on your panel, which mean the setBounds(...) code is completely unnecessary since the layout manager will reset the size and location of the scrollpane based on the rules of the layout manager.


As you described here, I really cannot see how it could work. You said it will "reset the size based on the rules of the layout manager", but what that really means ? I tried without setBounds, and it is not working. I have textArea and I put it on the scrollPane, so I have to define coordinates where I would like this component to arise.

At first I haven't realized that scrollPane is higher in hierarchy, so to say. I assumed wrong, that I should add schroll bar to textArrea. After short time, and talking with you, guys, I sow the difference.

Thank you for pointing out, though, setVisible. I started to use it always, since sometimes components was not visible. Now it is more clearer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding ScrollBar to JTextArea