Results 1 to 5 of 5

Thread: Control Arrays in Java?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Control Arrays in Java?

    Im trying to create a control array similar to what can be done 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 do 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 using 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.
    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(); 
       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()); 
      }
    }

  2. #2

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I reworked my project but im running into a problem with the lines below. For some reason im getting an output of nine components when there are infact eleven components contained within the JLayeredPane. These two lines are just for temporary
    purposes to see if in fact i am getting the right component count.
    Code:
    int num = jlp.getComponentCountInLayer(jlp.highestLayer());
    System.out.println(num);
    Now for these two lines. The first one i am getting an incompatible type error which i don't seem to understand at all since all i am doing is setting the size of an array to what a method is returning. For the second line i am getting an Incovertible type error which i don't understand either since JComponent is a subclass of Component i should be able to cast up the inheritance hierarchy.
    Code:
    Component comps = new Component[jlp.getComponentCountInLayer(jlp.highestLayer())];
    comps = (Component)jlp.getComponentsInLayer(jlp.highestLayer());
    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 = 15;
       int x = 0; 
       int y = 10;
    
       JTextField[] jtc = new JTextField[10];  
       JFrame jf = new JFrame("Data Form");
       JButton jb = new JButton("Check Fields");
       final JLayeredPane jlp = jf.getLayeredPane(); 
       jf.setSize(500,400); 
     
    
      jb.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent ae){
       int num = jlp.getComponentCountInLayer(jlp.highestLayer());
          System.out.println(num);
    
        //  Component comps = new Component[jlp.getComponentCountInLayer(jlp.highestLayer())];
        //  comps = (Component)jlp.getComponentsInLayer(jlp.highestLayer()); 
        }
       });
    
    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]);
           } 
          }
     jb.setBounds(new Rectangle((x + width + 10),y, width, height));
     jlp.add(jb);   
     jf.setVisible(true); 
     }
    }

  3. #3

  4. #4
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144

    Re: Control Arrays in Java?

    I can explain why the last two lines are causing icompatible errors...
    Code:
    Component comps = new Component[jlp.getComponentCountInLayer(jlp.highestLayer())];
    On the left hand side (of the =) your creating a pointer to a component, on the right your creating an array of components.

    That part should be...
    Code:
    Component[] comps = new Component[jlp.getComponentCountInLayer(jlp.highestLayer())];
    Changing this should also fix the line below!

    Hope that helps!
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Thanks for the help Mrs Kensington. I changed the following lines
    to the ones down below and i get a clean compile. Next ill have to look into why the component count is off.
    Code:
    Component comps = new Component[jlp.getComponentCountInLayer(jlp.highestLayer())];
    comps = (Component)jlp.getComponentsInLayer(jlp.highestLayer());
    Code:
    Component[] comps = new Component[jlp.getComponentCountInLayer(jlp.highestLayer())];
    comps = (Component[])jlp.getComponentsInLayer(jlp.highestLayer());

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