Article View: borland.public.jbuilder.java.swing
Article #10683Re: How to know the size in pixels???
From: Hongbo Su
Date: Thu, 19 Aug 1999 00:00
Date: Thu, 19 Aug 1999 00:00
85 lines
3123 bytes
3123 bytes
This is a multi-part message in MIME format.
--------------907AB29C6EAAAC260E4CAFC9
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Hi, Paul,
Is there anyway I can keep the size fixed?
For example, if I want to keep the 6 JLabels has the same width, but
the strings of these 6 Jlables are different? Thanks.
Hongbo
"Paul Furbacher [TeamB]" wrote:
> David V�squez Estrada wrote:
> >
> > nlaan wrote:
> >
> > > Sorry, my mistake the correct method is used as followed:
> > >
> > > public void paint(Graphics g)
> > > {
> > > FontMetrics fm = g.getFontMetrics();
> > >
> > > int width = fm.stringWidth("Hello World");
> > >
> > > int height = fm.getHeight();
> > > }
> > >
> >
> > Should I override the method paint of "some" kind of class or I can get an
> > instance of the Graphics class, since the graphics class is also an abstract
> > class.
>
> There are a number of ways to get what you want without
> having access to a graphics context as in the example above.
> Assuming a JLabel called 'myLabel':
>
> FontMetrics fm = myLabel.getFontMetrics(myLabel.getFont());
>
> int width = fm.stringWidth(myLabel.getText());
>
> // etc.
>
> You can even grab a graphics context
>
> Graphics g = myLabel.getGraphics();
> FontMetrics fm = g.getFontMetrics(myLabel.getFont());
> // ...
>
> However, when you are done with this, call
>
> g.dispose();
>
> I forgot about doing this in a library I recently wrote and someone
> wasn't very happy when he found a big memory leak! Oops, it
> just sort of slipped my mind ....
>
> But don't call 'g.dispose()' in a method which gets its 'g' as a method
> parameter. You do that, and boom, any code which shares that graphics
> context ends up trying to work with a null pointer.
>
> Paul Furbacher (TeamB)
--------------907AB29C6EAAAC260E4CAFC9
Content-Type: text/x-vcard; charset=us-ascii;
name="hsu.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Hongbo Su
Content-Disposition: attachment;
filename="hsu.vcf"
begin:vcard
n:Su;Hongbo
x-mozilla-html:FALSE
org:Gene Logic Inc.
adr:;;708 Quince Orchard Rd.;Gaithersburg;Maryland;20878;USA
version:2.1
email;internet:hsu@genelogic.com
title:Software Engineer
fn:Hongbo Su
end:vcard
--------------907AB29C6EAAAC260E4CAFC9--
Technical Details
Message-ID:
<37BC1748.941A235A@genelogic.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<37B18BC5.FDC58523@ucbcba.edu.bo> <7ovhp2$g7u11@forums.borland.com> <37B43577.DF127B44@ucbcba.edu.bo> <7p1peb$ijd1@forums.borland.com> <37B9C689.E8094E77@ucbcba.edu.bo> <37B9DDB8.E8BF0C80@bilbo.bio.purdue.edu>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 111d45,db95dab94e8fcb9e X-Google-Attributes: gid111d45,public From: Hongbo Su <hsu@genelogic.com> Subject: Re: How to know the size in pixels??? Date: 1999/08/19 Message-ID: <37BC1748.941A235A@genelogic.com> X-Deja-AN: 514695928 References: <37B18BC5.FDC58523@ucbcba.edu.bo> <7ovhp2$g7u11@forums.borland.com> <37B43577.DF127B44@ucbcba.edu.bo> <7p1peb$ijd1@forums.borland.com> <37B9C689.E8094E77@ucbcba.edu.bo> <37B9DDB8.E8BF0C80@bilbo.bio.purdue.edu> X-Accept-Language: en Content-Type: multipart/mixed; boundary="------------907AB29C6EAAAC260E4CAFC9" Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing