Article View: borland.public.jbuilder.java.swing
Article #12776Re: ViewportLayout bug? (was Re: JList size in a JScrollPane?)
From: Paul Furbacher
Date: Mon, 16 Nov 1998 00:00
Date: Mon, 16 Nov 1998 00:00
71 lines
3933 bytes
3933 bytes
Dave Howorth originally 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? > > (2) If I change the data being displayed in the listbox, using: > jList.setModel(aDifferentModel); > the size of the list does not change. If my new list is twice as long as the > old one, the scrollbars are unchanged and won't let me scroll past the first > half of the list. If I put the focus on the list and use cursor-down, it > will let me scroll down the list but just displays grey nothingness! Later, Dave showed some code which included the following line: > jList.setPreferredSize(new Dimension(50, 50)); /* THE PROBLEM */ Dave finally wrote: > I'm now convinced this is a bug in com.sun.java.swing.ViewportLayout.java. > > A little more experimenting has shown [...] Perhaps it's a bug with JViewport. However, I can effectively and un-problematically use JList inside a JScrollPane. Your problem stems from the attempt to set the preferred size of the jList component. I suspect you are doing this in an attempt to render a minimally sized scroller. Without somehow adjusting the width (and possibly the height) of the scroller, it's likely that you'll get an anemically sized listbox (here, a listbox is the sum of a JList nested in a JScrollPane). What you want to do is not to size the list, but the scroller. You always want to allow the JList view to be resizable according to its item-depth. By setting the preferred size of the JList component, you are in effect saying to the layout manager, "even if I don't have enough items to make me 100 pixels deep, DO IT ANYWAY." Instead, you should be telling the container (i.e., the JScrollPane, or scroller, for short) that it should be layed out with a preferred (initial) size of 100 x 100. That works much better. If the user decides to resize the window in which the scroller is contained (assuming you are using a gridbag layout), the advised preferred size is then used as a factor, not a rule, by the layout manager. 2) If you change the model contents, you must invoke the following methods in order to coordinate the view hierarchy. Here's what I do: // assuming JList list // JScrollPane scroller list.setListData(...); list.getParent().invalidate(); // tell viewport to invalidate itself scroller.validate(); scroller.repaint(); Yes, this seems to be more than might be necessary -- a lot of manual updating -- but Swing generally assumes that you are going to take on this "view coordinating" work in order to create "specialized" components. That said, it seems to me that a listbox is so universal that they probably should have gone a little further to create a "finished" component. Paul Furbacher
Technical Details
Message-ID:
<364FBE5E.493DEE04@bilbo.bio.purdue.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<727hon$ov14@forums.borland.com> <72fu3o$d5n28@forums.borland.com> <364D2037.7227@flash.net> <72ni1c$ob11@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: Paul Furbacher <pf@bilbo.bio.purdue.edu> Subject: Re: ViewportLayout bug? (was Re: JList size in a JScrollPane?) Date: 1998/11/16 Message-ID: <364FBE5E.493DEE04@bilbo.bio.purdue.edu> X-Deja-AN: 412292512 Content-Transfer-Encoding: 7bit References: <727hon$ov14@forums.borland.com> <72fu3o$d5n28@forums.borland.com> <364D2037.7227@flash.net> <72ni1c$ob11@forums.borland.com> To: Dave Howorth <5bx@iname.com> Content-Type: text/plain; charset=us-ascii Organization: Biological Sciences, Purdue Univ. Mime-Version: 1.0 Reply-To: pf@bilbo.bio.purdue.edu Newsgroups: borland.public.jbuilder.java.swing