Results 1 to 5 of 5

Thread: Loop in all JTextField for example

  1. #1
    DaoK
    Guest

    Loop in all JTextField for example

    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

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

    Code:
     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
      }
    }

  3. #3
    DaoK
    Guest
    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

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  5. #5
    DaoK
    Guest


    Forget it it's to hard lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width