Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding scrollbar to flowlayout

 
Juanjo Soyyo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to adding a scrollbar to a jpanel with flowlayout but is imposible, i don't see the scrollbar. I've tried a thousand different ways but I have not accomplished anything. I am fairly new to java and I have yet many failures so please be kind.

Here is my code:



Please help me and thanks for all.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A scroll bar belongs to a JScrollPane, not a JPanel. You need to add the JScrollPane to the JPanel.
 
Juanjo Soyyo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The jscrollpane is added to the jpanel:

jScrollPane2.setViewportView(jpanelProductos);

jPanel1.add(jScrollPane2);

is this what you wanted to say?.

Thanks for thr reply.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default the scroll bars appears "when needed".

You can explicitly specify/add the scroll bar using the constructor or setXXXScrollBarPolicy(int)



The java tutorial page on JScrollPane describes more
 
Juanjo Soyyo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the scroll appears only when needed, i see the tutorial and I've tried everything but not working for me. That's incredible. I have read over and over again but do not know how to display the scroll. I said before that I am very new to java. Could you guide me a little more ??.You can give me some more information?.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that the component inside the scroll pane has exactly the same dimensions as the scroll pane - why do you expect scroll bars to be shown?
 
Juanjo Soyyo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JScrollPane dimensions:

jScrollPane2.setBounds(0, 0, 410, 300);

Jpanel inside dimensions:

jpanelProductos.setPreferredSize(new java.awt.Dimension(400, 800));

i think the panel not have the same dimensions so I think that's not the problem.

is this what you wanted to say?.

Thanks for thr reply.
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you were told in the other forum where you posted the question:

1. Don't use a null layout
2. Don't use setPreferredSize().

Swing layout managers will do all the work for you so there is no need to do either of the above.
 
Juanjo Soyyo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i posted in this other forum:

I tested with getPreferedSize() and nothing, I can't get the scroll, probably I'll do something wrong. I have read over and over again but do not know how to display the scroll. I said before that I am very new to java. Could you guide me a little more ??.You can give me some more information?.

Sorry for my english and thanks for your reply.
 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juanjo Soyyo wrote:I like to adding a scrollbar to a jpanel with flowlayout but is imposible, i don't see the scrollbar. I've tried a thousand different ways but I have not accomplished anything. I am fairly new to java and I have yet many failures so please be kind.

Here is my code:



Please help me and thanks for all.


Hi
please try the following simple example .later you may include as needed.

Please reply
Thanks
As
CRMK






 
Juanjo Soyyo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
Thanks for all, the code is working now, i delete all code and make all of new and now all is working, here is my new code:



I only have a small problem, the scrollbar is always visible and i like view only when needed, I know is that the JPanel is bigger than the scrollpanel but I don't know how to grow as they grow products. If you can give me any ideas I'll be very grateful.

Thanks for all.
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tested with getPreferedSize() and nothing,


No, that is not what has been suggested!!!


Do NOT use a null layout.

Do NOT use the setPreferredSize() method.

All Swing components will determine there own preferred size. The layout manager will then determine the preferred size of the panel.
 
Piet Souris
Rancher
Posts: 1641
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
(...)
Do NOT use the setPreferredSize() method.

Why? There are situations where you know better than the LayoutManager as to what
the size should be. Just make sure that the LM is one that respects the preferred sizes.

This topic is such an example. You have a panel inside a scrollpane, and for some reason
you want this panel to be larger than the scrollpane, and larger than the LM would
make it.

I didn't understand the problem in this topic, so I wrote a small application with
a frame, a panel with FlowLayout and three buttons, all inside a smaller scrollpane.
No problem, but unfortunately (?) just before I wanted to reply I noticed that OP
had already solved his problem. Well, that's life.

@Juanjo
if you want to let your panel grow over time, just add more components to it,
as and when suitable, just do a 'panel.revalidate()' after such an adjustment.

Greetz,
Piet
 
Comal Rajagopalaratnam Muthukumar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Please note that Scroll Panes are created from a class ScrollPane.The object of scrollpane class can be created:-

private ScrollPane d;

where d = object of scrollPane Class scroll pane constructor can be evoked by
d= new ScrollPane();
The scroll pane is added to applet(as for example) by

d.add(c);

c=object of canvas canvas class

This sets a size of scroll pane objects

d.set size(300,300);

Pl reply if this suffices you to get what you need
Thanks
As
CRMK

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Comal,

1) Please do not unnecessarily quote the previous topic in its entirety. I have edited out the fluff from your topic.
2) ScrollPane and JScrollPane are AWT and Swing respectively. AWT reached its EOL years back. Nowadays you should be using only Swing
3) Ditto Applet and JApplet*
*I do not understand why you mentioned applets when the OP's code is using a JFrame. It might be a good idea to avoid unnecessary confusion by sticking to what the OP has provided
 
Juanjo Soyyo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
Many thanks to all, you've helped me a lot, every one of the answers has help me a little until finally I got it all sorted and running. Many, many thanks.

I put the final code if anyone wants to see:



thank you very much to all of you who have put a response trying to help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!