-
how to get the components at runtime
i hava some JTextFields and JLables in a class that extends JPanel. now if
i want to set the font property of all of them with minimul coading what
should i do? i know that i can set it by using the method setFont(Font f)
but i do not want to write this code for all my components. i would like
to use a loop to finish it. how do i do that??
-
Re: how to get the components at runtime
I wouldn't use a loop, I'd just use a class that extends JTextField (for
example) and includes "setFont(...)" in its constructor, then create objects
of this class to add to the panel. I'm assuming that you want them all to
use the same font.
PC2
kiran <bhava_kiran@rediffmail.com> wrote in message
news:3a48aa68$1@news.devx.com...
>
>
> i hava some JTextFields and JLables in a class that extends JPanel. now if
> i want to set the font property of all of them with minimul coading what
> should i do? i know that i can set it by using the method setFont(Font f)
> but i do not want to write this code for all my components. i would like
> to use a loop to finish it. how do i do that??
>
-
Re: how to get the components at runtime
Kiran,
The fact that all of these GUI classes extend the Component class makes
this very easy to do. First, create an array of Components (the function
getComponents() returns just such an array (however, if you have components
that hold other components, like Panels, you'll have to do this for each
of them)). Second, simply use the following for loop (assuming that you
have the Component array which we will call comps[]):
for(int i = 0; i < comps.length; i++)
{
comps[i].setFont(Font f);
}
The function setFont() is in the Component class. However, even if each
of your components had a different setFont() method (ie. if you have to override
the Component setFont() method), this code would still work.
Three cheers for Object Oriented Programming!
Shayne
"kiran" <bhava_kiran@rediffmail.com> wrote:
>
>
>i hava some JTextFields and JLables in a class that extends JPanel. now
if
>i want to set the font property of all of them with minimul coading what
>should i do? i know that i can set it by using the method setFont(Font f)
>but i do not want to write this code for all my components. i would like
>to use a loop to finish it. how do i do that??
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks