PDA

Click to See Complete Forum and Search --> : Loop in all JTextField for example


DaoK
Apr 22nd, 2002, 09:29 PM
Is there a way to make a loop that will check all JTextField, all jTextArea ... for example...

The idea is that I have about 20 jTextField and I wanted to init them all at "0".

Any ways to do it faster that manually one by one ?

thx you,
Daok

Dillinger4
Apr 23rd, 2002, 02:25 PM
It sounds like you have multiple text fields on a form and you want to check each one for a certain value. Sort of like using a control array in Visual Basic. I think this would be the way to go but i have hit a road block on the last lines of code. What i wanted to was create a component array set to the size of what the JLayeredPane getComponentCountInLayer(int layer) method returns then get the actual components in the layer usign JLayeredPanes getComponentsInLayer(int layer) method which is supposed to return a Component array. Then loop through the Component array and test each component in that array to see if they are an instance of a JTextComponent. Once that is done we can take the data validation from there. But the last two lines of code genterate incompatiable types errors.

I have to go to work now but see if can you can play around with this alittle and ill try and fix it later on tonight. :)


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


class DataForm {
public static void main(String[] args){


int height = 40;
int width = 100;
int space = 10;
int x = 0;
int y = 10;


JTextField[] jtc = new JTextField[9];
JLayeredPane jlp = new JLayeredPane(); // layout property is set to null by default
JFrame jf = new JFrame("Data Form");


for(int i = 0; i < jtc.length; ++i){
jtc[i] = new JTextField();
}

for(int i = 0; i < jtc.length; ++i){
if(x == 0){
x = 10;
jtc[i].setBounds(new Rectangle(x, y, width, height));
jlp.add(jtc[i]);
continue;
}
x += (width + space);
jtc[i].setBounds(new Rectangle(x, y, width, height));
jlp.add(jtc[i]);

if(x > (jf.getWidth() - (width + space))){
x = 10;
y += (height + space);
jtc[i].setBounds(new Rectangle(x, y, width, height));
jlp.add(jtc[i]);
}
}
jf.setSize(500,400);
jf.setVisible(true);

Component comps = new Component[jlp.getComponentCountInLayer(jlp.highestLayer())];
comps = jlp.getComponentsInLayer(jlp.highestLayer()); // note highest numbered layers are first
}
}

DaoK
Apr 23rd, 2002, 05:02 PM
Thx you but I do not want to change all my code to put all button in array. hummm I know that in Visual Basic we can loop through all TextBox, perhaps Java do not have that feature.

Thx for the time,
Daok

Dillinger4
Apr 23rd, 2002, 11:19 PM
Daok whatever you do your probably going to have to invoke a method that returns all or some of the components that are held within a top level component. :)

DaoK
Apr 24th, 2002, 07:44 AM
:eek:

Forget it it's to hard lol