"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.
|...But....
| 1) How does that help me post neatly formatted code;
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