what is the javax.swing equivalent of java.awt.getComponents() method

Discussion in 'Java' started by african brewer, Aug 10, 2009.

  1. Hi all,
    I have a small problem,i want to add all my text fields in a panel to
    a collection.
    I also want to be able to access the text in those text fields later
    on while i traverse my collection.
    I dont want to add the text fields one after another,i want to be able
    to add them wholesale like this

    Component[] component= pnlMiddle.getComponets();

    The problem with the above way of doing it is that the array will be
    hold java.awt components instead of javax.swing components of which
    JTextField is a subclass.

    My question is: is there a method that i can use instead of
    java.awt.getComponents()
    something like
    JTextField[] component= pnlMiddle.getComponets();
     
    african brewer, Aug 10, 2009
    #1
    1. Advertising

  2. african brewer

    Qu0ll Guest

    "african brewer" <> wrote in message
    news:...
    > Hi all,
    > I have a small problem,i want to add all my text fields in a panel to
    > a collection.
    > I also want to be able to access the text in those text fields later
    > on while i traverse my collection.
    > I dont want to add the text fields one after another,i want to be able
    > to add them wholesale like this
    >
    > Component[] component= pnlMiddle.getComponets();
    >
    > The problem with the above way of doing it is that the array will be
    > hold java.awt components instead of javax.swing components of which
    > JTextField is a subclass.
    >
    > My question is: is there a method that i can use instead of
    > java.awt.getComponents()
    > something like
    > JTextField[] component= pnlMiddle.getComponets();


    JTextField is a subclass of both java.awt.Component and
    javax.swing.JComponent so you can just use getComponents() and then cast
    them as required.

    --
    And loving it,

    -Qu0ll (Rare, not extinct)
    _________________________________________________

    [Replace the "SixFour" with numbers to email me]
     
    Qu0ll, Aug 10, 2009
    #2
    1. Advertising

  3. african brewer

    markspace Guest

    Re: what is the javax.swing equivalent of java.awt.getComponents()method

    african brewer wrote:

    > My question is: is there a method that i can use instead of
    > java.awt.getComponents()
    > something like
    > JTextField[] component= pnlMiddle.getComponets();



    Qu0ll is probably right, you really should use the Components[] array.
    A JPanel is a generic container and can hold any type. You will almost
    certainly want someday to add a different component. The following loop
    will work fine for you:

    for( Component comp : component ) {
    if( comp instanceof JTextField ) {
    // do JTextField stuff...
    }
    }

    However if you like living dangerously the following code snippet will
    convert your Component array to JTextFields:

    public static void main( String[] args )
    {
    JPanel panel = new JPanel();
    Component[] component = panel.getComponents();
    JTextField[] textFields = Arrays.copyOf( component,
    component.length, JTextField[].class );
    }

    (Syntax checked, but not tested. This WILL throw errors if you have
    anything that does not subclass JTextField in the component array.)
     
    markspace, Aug 10, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jochen Opp

    java.awt or javax.swing

    Jochen Opp, Sep 23, 2003, in forum: Java
    Replies:
    1
    Views:
    595
    Roedy Green
    Sep 23, 2003
  2. Jochen Opp
    Replies:
    0
    Views:
    375
    Jochen Opp
    Sep 24, 2003
  3. Jochen Opp
    Replies:
    2
    Views:
    683
    Chris Smith
    Sep 25, 2003
  4. lizard
    Replies:
    0
    Views:
    1,784
    lizard
    Jan 30, 2006
  5. steveh44
    Replies:
    12
    Views:
    771
    Daniele Futtorovic
    May 6, 2011
Loading...

Share This Page