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.)