• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Adding ScrollBar to JTextArea

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 28290
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add the scroller to the panel, not the text area. (See the comment at line 10? Don't do that.)
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I changed to:



But still I cannot see plainTextArea, it is not visible ?
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you add a JScrollBar to the JScrollPane? It takes care of the scroll bars itself.
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't know that..Ok, but still I don't know why this code is not working ?
 
Paul Clapham
Marshal
Posts: 28290
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28290
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28290
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is with null layout, still:



It wooorks!
 
Rob Spoor
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
groupBoxEncryption does not have a null layout; JPanels use a FlowLayout by default.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


And JInternalFrame (for completeness).
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
It's exactly the same and completely different as this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic