how to get the components at runtime


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: how to get the components at runtime

  1. #1
    kiran Guest

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


  2. #2
    Paul Clapham Guest

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




  3. #3
    Shayne Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center