If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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??
>