Article View: borland.public.jbuilder.java.swing
Article #12763Re: JList size in a JScrollPane?
From: Ross M. Dukeshier
Date: Sat, 14 Nov 1998 00:00
Date: Sat, 14 Nov 1998 00:00
97 lines
4462 bytes
4462 bytes
Dave Howorth wrote:
>
> I wrote:
> >(1) When I make the dialog taller (as a user, dragging it at runtime), the
> >list [I assume] expands, adding white space after the end of the list. This
> >is fine. But when I then make the dialog box slightly shorter, a scroll bar
> >is added and I can scroll to a completely blank area. What should happen,
> >IMHO, is for the JList to remove the blank space and just become
> 'naturally'
> >shorter, still with no scroll bar. What am I missing?
>
> I've got a little further with this, and I attach some simple source below
> that demonstrates the problem. It doesn't seem to matter what the window
> type is, nor what layout manager is used. A standard JB app shell creates a
> MyFrame. If the line marked /* THE PROBLEM */ is commented out, it runs OK;
> with it in, it shows the problem above.
>
> Can anybody explain this to me, please?
>
> Thanks, Dave
>
> package ListTests;
>
> import java.awt.*;
> import java.awt.event.*;
> import com.sun.java.swing.*;
>
> public class MyFrame extends JFrame
> {
> //Construct the frame
> JList jList = new JList(new String[] {"one", "two", "three"});
> JScrollPane jScrollPane = new JScrollPane();
>
> public MyFrame()
> {
> enableEvents(AWTEvent.WINDOW_EVENT_MASK);
> try
> {
> jbInit();
> }
> catch (Exception e)
> {
> e.printStackTrace();
> }
> }
>
> //Component initialization
> private void jbInit() throws Exception
> {
> jList.setPreferredSize(new Dimension(50, 50)); /* THE PROBLEM */
> jScrollPane.getViewport().add(jList, null);
> this.getContentPane().add(jScrollPane, null);
> }
>
> //Overriden so we can exit on System Close
> protected void processWindowEvent(WindowEvent e)
> {
> super.processWindowEvent(e);
> if (e.getID() == WindowEvent.WINDOW_CLOSING)
> System.exit(0);
> }
> }
>
> /*****************/
>
> PS I went to the bookshop yesterday and looked at the O'Reilly Java Swing
> book. Naturally, I looked at their first JList example and spotted a bug
> (they didn't bind the model to the view). Didn't inspire much confidence in
> the book :-( Are there any better ones?
If you use the GridBagLayout and tell the list not to grow, it won't
adjust with the scrollpane - if you increase its size it will leave
space at the bottom and if you decrease its size (shorter than the
list), it will add the scrollbar.
If that didn't help, it sounds like it may be adjusting the preferred
size of the list differently depending on whether you're increasing or
decreasing the size of the scrollpane. I'd add some code to print out
the three sizes of both components in the execution log when you click
on a button on that screen. Then you should be able to see what it's
doing with them. Keep in mind, the list won't grow bigger than the
maximum size or shrink smaller than the minimum size. If you're
adjusting the scrollpane smaller than the minimum size of the list, it's
going to show the scrollbar. Likewise the other way around.
Hope it helps.
Ross Dukeshier
P.S. As far as the O'Reilly book, I've heard good things about it. If
you don't already have them, I found the Java in a Nutshell and the Java
in a Nutshell examples books provide a good foundation, nothing
extravagant though. They also have some things that don't work in them,
but I've found that's because Java changed. Don't know if it relates to
what you found in O'Reilly, but how the models work with JTable changed
in Java. They may have for JList as well. Don't know all the details
of the changes - just started Java this past year so I'm a couple years
behind the times.
Technical Details
Message-ID:
<364D2037.7227@flash.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<727hon$ov14@forums.borland.com> <72fu3o$d5n28@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,1177ab7a6295220d X-Google-Attributes: gid111d45,public From: "Ross M. Dukeshier" <dukeman@flash.net> Subject: Re: JList size in a JScrollPane? Date: 1998/11/14 Message-ID: <364D2037.7227@flash.net> X-Deja-AN: 411712588 Content-Transfer-Encoding: 7bit References: <727hon$ov14@forums.borland.com> <72fu3o$d5n28@forums.borland.com> Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Reply-To: dukeman@pop.flash.net Newsgroups: borland.public.jbuilder.java.swing