Forums Register Login

JPanel to scollable panel

+Pie Number of slices to send: Send
Hi
I am dynamically adding elements into JPanel. When the number of elements grow, I am not able to see them. What should I do if I want to change this into scrollable panel. As the application is almost complete with JPanel, now this situation arises. I tried changing to JPanel component into JScrollablePane. But my dynamically added components are not visible in the UI. Can anyone suggest a solution for this.
[ April 07, 2008: Message edited by: Gopu Akraju ]
+Pie Number of slices to send: Send
Instead of adding the panel directly to its parent, use a JScrollPane between them.

So instead of , use
+Pie Number of slices to send: Send
Thanks for your response. I tried it and it works now. But the added components are added in the same line hence I need to use horizontal bar, but I want to use horizontal bar. How do I control it. Thanks.
I tried as below:


[ April 07, 2008: Message edited by: Gopu Akraju ]
+Pie Number of slices to send: Send
I even tried as below:


In htis case I am not seeing a horizontal bar but vertical bar doesn't appear. Thanks.
+Pie Number of slices to send: Send
I tried ALWAYS for vertical bar.
But I think the problem is some where else. Basically I am adding dynamic elements into JPanel dPanel and finally adding it to scrollable Pane. When I was just using JPanel, components were added line by line. I didn't set any layout manager for JPanel initially. And the components(set of JLabel followed by some JCheck boxes) were added line by line. But when I tried adding dPanel inside the scrollable pane, the components are added in one single line and what might be the problem. Anything to do with layout manager? Not sure.
+Pie Number of slices to send: Send
 

Originally posted by Gopu Akraju:
I tried it and it works now. But the added components are added in the same line hence I need to use horizontal bar, but I want to use horizontal bar. How do I control it. Thanks.
I tried as below:




It sounds like the behavior of dPanel's layout manager is to place
everything on one line. You could try

dPanel.setLayout(new GridLayout(0, N));

where N is how many objects you would want to see on each line.
That may cause other layout problems, though, depending on what
exactly you are trying to do.

[edit: The Container in question is dPanel, not the content pane.
Sorry about that.]
[ April 07, 2008: Message edited by: Brian Cole ]
+Pie Number of slices to send: Send
 

Originally posted by Brian Cole:
It sounds like the behavior of dPanel's layout manager is to place everything on one line.


Unless another layout manager is specified, a JPanel has a FlowLayout which does just that. It only wraps when there is no more space for components. This will never happen within JScrollPanes though.

I had this problem once as well, so I wrote a class to fix it. It uses the JViewport's size to limit itself on. As such, there is only a need for horizontal wrapping if a component doesn't fit in the panel at all. For the rest it just uses the same way of laying out components as FlowLayout.
+Pie Number of slices to send: Send
Hi Rob,

Can you explain bit more on how you fixed through writing c separate class on fixing this. You are right, with flow layout and horizontal bar of scrollable pane, componenets are added side ways always. I tried box layout but the list becomes too big and not nice to look with vertical bar. I can't go for Gridlayout as the number of components vary a lot. I have a set of label followed by number of check boxes. For each label, the number of check boxes vary. Thanks your time again.
+Pie Number of slices to send: Send
 

Originally posted by Gopu Akraju:
I can't go for Gridlayout as the number of components vary a lot.



You shouldn't use GridLayout if it doesn't suit you, but that the
number of components varies isn't in itself a reason not to use it.

new GridLayout(0, N) // replace N with the number of columns

By specifying zero for the number of rows, it will allow you to add
any number of Components and lay them out in exactly enough
rows to fit them.

This usually works pretty well in a JScrollPane. (Unless the viewport
is larger than the preferred size, in which case they will be stretched
too tall. Better in that case to put a BorderLayout panel in the JScrollPane
and the GridLayout panel in the NORTH of the BorderLayout panel.)

The usual problem with GridLayout, of course, is that it stretches
the Components to be as big as a cell of the grid. This might be ok
for labels and check boxes but it can look pretty bad for things like
buttons unless you interpose an intermediate panel.

[edit: mention the viewport-too-large situation]
[ April 07, 2008: Message edited by: Brian Cole ]
+Pie Number of slices to send: Send
 

Originally posted by Gopu Akraju:
Hi Rob,

Can you explain bit more on how you fixed through writing c separate class on fixing this. You are right, with flow layout and horizontal bar of scrollable pane, componenets are added side ways always. I tried box layout but the list becomes too big and not nice to look with vertical bar. I can't go for Gridlayout as the number of components vary a lot. I have a set of label followed by number of check boxes. For each label, the number of check boxes vary. Thanks your time again.



This code will make sure that the preferred width will only be larger than the JViewport's width if it is really necessary.
+Pie Number of slices to send: Send
Thanks Rob for your help. I tried many different kids of solution and finally came to know that it is BUG. Flowlayout(dPanel) inside a scrollabale pane is kind of annoying. Sorry to bother you after a long time. I tried your code but it is getting displayed continously as below:
All these components are inside a JPanel which is inside a scrollable panel.

Labe1 chbk1 chbk2 chbk3 Label2 chbk1
chbk2 chbk3 chbk4 Label3 chbk1 chbk2
Label4 chbk1 chbk2

But I need my display like this:
Label1 chbk1 chbk2 chbk3
Label2 chbk1 chbk2 chbk3 chbk4
Label3 chbk1 chbk2
Label4 chbk1 chbk2

Every line starts with Label but the number of chbk might vary.
One of the solurtion which I tried was setting the preferred size as below:



The above said code also wraps up and trying to print as below as the size of the label changes.


And another problem with the setPreferred size of the panel is, I am able to see the vertical bar for this panel but not the horizontal bar. Now that whole application is done, and am finding problem in the display. Hence if I change the dPanel layout from flowLayout to something else, then it is very difficult to change the application.

Is there a way to solve this prolem. I want to print everyline starting with Label folowed by chbks with horizontal bar. Please help me.
+Pie Number of slices to send: Send
Hi Gopu

I too came across same problem as yours above.
I have one sugesstion to you
in order to place the element one after another
as you want just set Layout to null first.



And now when you place checkboxes or labels in panel
set some position for them i mean x-axis and
y axis value like (100,50) etc.
.

I hope it helps you to some extent.

SCJP(1.5),SCWCD(On the way)
Dhwani:>Winning is not important but it is the only thing.
+Pie Number of slices to send: Send
 

Originally posted by dhwani mathur:
Hi Gopu

I too came across same problem as yours above.
I have one sugesstion to you
in order to place the element one after another
as you want just set Layout to null first.



And now when you place checkboxes or labels in panel
set some position for them i mean x-axis and
y axis value like (100,50) etc.
.

I hope it helps you to some extent.


It won't help you - the panel will not have a correct preferred size and therefore will not have any scrollbars at all. I have solved this myself by creating a class called NullLayout which does not do any laying out but only calculates the minimum and preferred size*, but even then it should not be used a lot. In fact, I only use it in panels with draggable labels in a Visio type of application. That's also the reason I created it. For all other purposes, there are much better layout managers.

I have pointed Gopu to the FormLayout by JGoodies, which seems to help him quite a bit. It has power similar to the GridBagLayout but is much easier to use.


* The preferred width of a panel would be the maximum of the X coordinate + the preferred width over all components; the height would be similar. No components would mean a preferred size of 0x0.
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2782 times.
Similar Threads
Increasing the size of the Panel
unable to see dynamically added component in swing
Layouts inside panel
dynamically adding a JPanel in JFrame - not reflecting
Realtime graph updates in Java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:37:17.