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

A

african brewer

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();
 
Q

Qu0ll

african brewer said:
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)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
M

markspace

african said:
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top