Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

JScrollPane containing a JPanel of JLabels, will not scroll.

Thread view: 
natG - 28 Jan 2004 23:45 GMT
Can you please advise as to why the JScrollPane returned from the following method
will not scroll?
I have tried numerous combinations of Layouts, of this and its containing
JFrame/JPanel.

<code>

/** Returns a JScrollPane containing a JPanel with vertical labels built from an
array of JLabels.*/

public final static JScrollPane buildPanelOfLabels(JLabel[] labels){

JPanel panel = new JPanel();

panel.setPreferredSize(new Dimension(200,310));

panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

//panel.setLayout(new GridLayout(0,1));

JScrollPane scrlP = new JScrollPane(panel);

scrlP.getViewport().add(panel);

for(int i=0;i<labels.length;i++){

panel.add(labels[i]);

}

return scrlP;

}

</code>

Please excuse if the code didn't format correctly.

Thanks

-nat
Andrew Thompson - 29 Jan 2004 00:41 GMT
| <code>
<...ahem. code SNIPPET    ;-) >

| /** Returns a JScrollPane containing a JPanel with vertical labels built from an
| array of JLabels.*/

| </code>

I did not see anything immediately wrong
with this snippet, but it is important to
see the bigger picture.  It might be easier
to answer if you supply an SSCCE
http://www.physci.org/codes/sscce.jsp

| Please excuse if the code didn't format correctly.

The SSCCE has some tips on that as well..

--
Andrew Thompson
* http://www.PhySci.org/codes/ Web & IT help
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Andrew Thompson - 29 Jan 2004 00:46 GMT
"Andrew Thompson" ...
| "natG" ...
|
| | <code>
| <...ahem. code SNIPPET    ;-) >

<..errr. snippet snipped, but not properly
notated as _being_ snipped.  Ooops!  >

| | </code>

<rest snipped ..already said
natG - 29 Jan 2004 01:01 GMT
> "Andrew Thompson" ...
> | "natG" ...
[quoted text clipped - 8 lines]
>
> <rest snipped ..already said>

My intent was that like many forums, <code></code> delimiters simply maintain
formatting.
There is nothing snipped. The entire problem is presented, in the single method
posted.
TA
-nat
natG - 29 Jan 2004 00:54 GMT
> | <code>
> <...ahem. code SNIPPET    ;-) >
[quoted text clipped - 21 lines]
> * http://www.1point1C.org/ 1.1C - Superluminal!
> * http://www.AThompson.info/andrew/ personal site

The on-line compiler is neat. But....
1) How does that help me post neatly formatted code;
2) I still dont know the answer to my post!

Thanks
-nat
Andrew Thompson - 29 Jan 2004 01:08 GMT
"natG" ...
| "Andrew Thompson" ...
...
| > see the bigger picture.  It might be easier
| > to answer if you supply an SSCCE
| > http://www.physci.org/codes/sscce.jsp

| The on-line compiler is neat.

Thanks :)

|...But....
| 1) How does that help me post neatly formatted code;

The main point of the SSCCE document is
_complete_, so we can run it easily and
see what you are doing (wrong).

The on-line compiler is the 'acid test'
for if your code is complete, if you can
copy/paste and compile it there without
errors it is 'self-contained'..

| 2) I still dont know the answer to my post!

Everything worthwhile takes time.

[ Did that sound, zen-like,
..or (bad) pun-ish?   ;-)  ]

--
Andrew Thompson
* http://www.PhySci.org/codes/ Web & IT help
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
natG - 29 Jan 2004 02:11 GMT
Ok. You win.
I compiled it online at your site. But was dissapointed that it didn't offer to
run it.
Anyhow, here is the complete simplified code.
(Please advise as howto retain formatted code on Usenet. tx)
<snip>
import java.awt.Dimension;

import javax.swing.BoxLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

public class ScrollingLabelsTest extends JFrame {

JLabel[] labels = null;

public ScrollingLabelsTest(int numOfLabels){

super(numOfLabels + " labels");

labels = new JLabel[numOfLabels];

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

initLabels();

getContentPane().add(buildPanelOfLabels(labels),"Center");

setSize(100, 250);

setLocation(100, 300);

pack();

setVisible(true);

}

private void initLabels(){

for(int i = 0; i < labels.length; i++){

labels[i] = new JLabel("Label Test " + i);

}

}

/** Returns a JScrollPane containing a JPanel with vertical labels built from an
array of JLabels.*/

public final static JScrollPane buildPanelOfLabels(JLabel[] labels){

JPanel panel = new JPanel();

panel.setPreferredSize(new Dimension(200,310));

panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

//panel.setLayout(new GridLayout(0,1));

JScrollPane scrlP = new JScrollPane(panel);

scrlP.getViewport().add(panel);

for(int i=0;i<labels.length;i++){

panel.add(labels[i]);

}

return scrlP;

}

public static void main(String[] args) {

new ScrollingLabelsTest(30);

}

}

<snip>

Thank you all.

I might repost in a new thread since many people have given up on me already<g>

-nat
Andrew Thompson - 29 Jan 2004 03:07 GMT
| Ok. You win.
| I compiled it online at your site. But was dissapointed that it didn't offer to
| run it.

..hmmm.  Odd.

Sorry you did not get a launchable app,
indeed, you should have.

Your code compiles cleanly, the link
at the top of the page after you compile
points to a jar file with manifest and
main class specified.

At the moment it does not work (at my site),
as it is supposed to allow you to launch the
app from that link, which definitely does _not_
work (could not find main class ..grrr).

I have downloaded the resulting .jar and
will have a closer look at it, it certainly
contains the manifest and class.
wanders off, mumbling..

--
Andrew Thompson
* http://www.PhySci.org/codes/ Web & IT help
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Andrew Thompson - 29 Jan 2004 04:06 GMT
| Ok. You win.

I think I have discovered the problem, everybody wins.

| <snip>

| import javax.swing.JScrollPane;
|
| public class ScrollingLabelsTest extends JFrame {

public ScrollingLabelsTest(int numOfLabels)
{
 super(numOfLabels + " labels");
 labels = new JLabel[numOfLabels];
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 initLabels();
 getContentPane().add(buildPanelOfLabels(labels),"Center");
// note that I pack() it..
 pack();

// _then_ setSize
 setSize(100, 250);
 setLocation(100, 300);

// before finally showing
 setVisible(true);
}
___________________________________
HTH

--
Andrew Thompson
* http://www.PhySci.org/codes/ Web & IT help
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
natG - 29 Jan 2004 17:12 GMT
> | Ok. You win.
>
[quoted text clipped - 23 lines]
>   setVisible(true);
>  }

Sorry. But still does not work. It just makes it seem like it works.
By that I mean, that although you get scroll bars, they are frame based
and not on the panel. You can only scroll down a little (based on panel to frame
relationship)
but still not scroll down to the last labels. The scrollbar needs to be based on
the height of all labels
relative to the size of its container.
By the way, you get these results without pack() altogether.
TA
-nat
Babu Kalakrishnan - 29 Jan 2004 12:31 GMT
> getContentPane().add(buildPanelOfLabels(labels),"Center");
>
[quoted text clipped - 3 lines]
>
> pack();

The pack() call above will invalidate the setSize() call just before
it.
(pack will force a setSize() on the frame in such a manner that all
the contents are displayed at their preferred sizes). That is the
reason
why the scrollbars don't appear in your example.

Moving pack() to before the setLocation call (like what Andrew did)
will
show up scrollbars, but that isn't the real solution either. (The same
effect can be achieved by just removing the pack() call.) On my
system,
the panel scrolls only to the 20th label or so - not to *all* the
labels
you added.

> public final static JScrollPane buildPanelOfLabels(JLabel[] labels){
>
> JPanel panel = new JPanel();
>
> panel.setPreferredSize(new Dimension(200,310));

The above line is the root cause of your grief. When you hard code a
value for the preferredSize, the automatic size recalculation
mechanism
which layout managers use is defeated. So in spite of how many labels
you
add to this panel, the JScrollPane thinks that its preferred size is
only
200 X 310 pixels, and it wiill try to display the extents of the panel
only
within that limits.

Simply comment out the setPreferredSize call, and the example should
work as intended.

BK
natG - 29 Jan 2004 17:21 GMT
> > getContentPane().add(buildPanelOfLabels(labels),"Center");
> >
[quoted text clipped - 18 lines]
> the panel scrolls only to the 20th label or so - not to *all* the
> labels  you added.

Yep. Noticed that. Guess I need to read-up on pack().

> > public final static JScrollPane buildPanelOfLabels(JLabel[] labels){
> >
[quoted text clipped - 13 lines]
> work as intended.
> BK

THANK YOU!!!!!!!!! That was it!

Question: Now, a step further, I will remove and/or add labels dynamically.
Do I call revalidate() on the a)panel, b)scrollpane, c)frame, or combination
thereof.

TA
-nat


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.