🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #891

Re: adding components to JPanel in JScrollPane

#891
From: John McGrath [TeamB]
Date: Sat, 06 Nov 2004 08:54
61 lines
3537 bytes
On 11/2/2004 at 2:48:31 PM, Alan Shiers wrote:

> At the very end of the jbInit() method I set up cardPanel to a
> GridLayout of 10 rows 1 column.  This is so that I can display
> up to 10 distinct components as I add them dynamically using the
> displayResults() method.

If you want to display *up to* 10 rows, try using GridLayout(0,1) instead
of GridLayout(10,1).  In GridLayout, either the rows or the cols argument
may be 0 (but not both).  This means that it will grow in that dimension
as needed.  When you specify (10,1), it will always make space for 10
rows, which will give you white space at the bottom if the actual count is
less than 10.

> As you can imagine, the new HandStats (which extend JPanel) if they are
> being added to cardPanel, are not being displayed.  I've tried using
> repaint() methods but they don't do anything. 

I do not think the problem is that the components are not being repainted,
but rather that they are not being laid out.  They are being painted, but
they have a size of [0,0], so you do not see anything.

There are two easy ways to recognize this problem.  One that will work
most of the time is to resize your frame.  Depending on the component
hierarchy, that will often force all components to be laid out, so they
will magically appear when you do this.

A more reliable method (but not quite as easy) is to take advantage of a
feature that it hidden in the Window class.  When you press Ctrl-Shift-F1
in a Window (which includes Frame and Dialog), the Window will do a list()
on itself to System.out, displaying the entire UI hierarchy.  You can see
the size and location of each component, as well as many other attributes.

The solution is to request that the container that you have modified lay
itself out again.  The best way to do that is to use the revalidate()
method, which marks the component's layout as being invalid, and then
posts a request to the event queue to revalidate it.  This has an
advantage over calling validate() directly, in that it has the effect of
coalescing multiple revalidate() calls.

So instead of the repaint() calls that you tried, use:

    cardPanel.revalidate();

> ?  Also, how do I remove the HandStats objects from cardPanel so
> that I can display another new set of them?

   cardPanel.remove( stats );

But you probably want to remove them all in one show, so you can use:

   cardPanel.removeAll();

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID: <xn0dpflbi781zaa002@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.maxwell.syr.edu!in.100proofnews.com!in.100proofnews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4189145f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,bd4e403bad384b6
X-Google-Attributes: gid111d45,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "John McGrath  [TeamB]" <ng-b@JPMcGrath.net>
Subject: Re: adding components to JPanel in JScrollPane
Newsgroups: borland.public.jbuilder.java.swing
References: <4189145f$1@newsgroups.borland.com>
User-Agent: XanaNews/1.16.4.6
Message-ID: <xn0dpflbi781zaa002@newsgroups.borland.com>
X-Ref: newsgroups.borland.com ~XNS:0000002C
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
NNTP-Posting-Host: 141.158.57.49
Date: 6 Nov 2004 01:54:26 -0700
X-Trace: newsgroups.borland.com 1099734866 141.158.57.49 (6 Nov 2004 01:54:26 -0700)
Lines: 61
X-Authenticated-User: jpmcgrath
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.maxwell.syr.edu!in.100proofnews.com!in.100proofnews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail