Article View: borland.public.jbuilder.java.swing
Article #1023Re: CardLayout
From: John McGrath [TeamB]
Date: Tue, 21 Sep 2004 01:46
Date: Tue, 21 Sep 2004 01:46
57 lines
3307 bytes
3307 bytes
On 9/14/2004 at 2:54:02 AM, Alex Stylianos wrote:
Sorry you have not gotten an earlier response. Many Borlanders and TeamB
members were at BorCon last week, and most TeamB members attended the
annual TeamB conference after that.
> > I am using JDK 1.4.2_01-b06 and the problem is that I cannot have a
> > CardLayout switch cards properly.
> > I have tried switching panels with the following 3 different ways:
> > 1) ((CardLayout)pnMain.getLayout()).show(pnMain, pn); // where pn is
> > the name of the new panel
This is the correct way to switch panes with a CardLayout.
> perhaps I should add that I have created my own version of JPanel
> because I want to have background images so I needed to override the
> paint() method.. the code for paint() is here (although I don't think
> it makes any difference for the problem):
>
> public void paint(Graphics g) {
I am not sure if this is your problem, but this is not the normal way to
do painting in a Swing application. In general, you should override the
paintComponent() method rather than the paint() method. paintComponent()
is responsible for painting just the main part of the JComponent, whereas
paint() handles lots of other things like painting the background,
borders, child components, etc.
> if (tracker != null && image != null) {
> if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) { }
> else if (tracker.statusID(imageID, true) == MediaTracker.COMPLETE)
This logic should not be necessary in most cases and it may be interfering
with the proper painting of the component. The
ImageProducer/ImageObserver infrastructure automatically deals with
waiting for the image to load and painting when it is has loaded.
Normally, all you need to do is to call drawImage().
> super.paint(g);
> }
When you override painting-related methods (e.g. paint, paintComponent),
you do want to call the superclass method from your override method, but
you should do this *first*, not last. Since the default implementation of
paint() paints the background under most circumstances, it will end up
painting over anything that you paint before it is called.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID:
<414f87ff$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newshosting.com!nx02.iad01.newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<41468990@newsgroups.borland.com> <4146958a@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,5ea5dd499f3c6a16 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit From: "John McGrath [TeamB]" <ng-b@JPMcGrath.net> Subject: Re: CardLayout Newsgroups: borland.public.jbuilder.java.swing References: <41468990@newsgroups.borland.com> <4146958a@newsgroups.borland.com> User-Agent: XanaNews/1.16.4.2 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 NNTP-Posting-Host: 141.151.8.31 Message-ID: <414f87ff$1@newsgroups.borland.com> Date: 20 Sep 2004 18:46:39 -0700 X-Trace: newsgroups.borland.com 1095731199 141.151.8.31 (20 Sep 2004 18:46:39 -0700) Lines: 57 X-Authenticated-User: jpmcgrath Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newshosting.com!nx02.iad01.newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail