Storing Array

Discussion in 'Mac Programming' started by Alvinkiang, May 2, 2006.

  1. macrumors newbie

    Joined:
    Apr 24, 2006
    #1
    adding 4 jTextfield will mess up what i have done Previously so i chose an array but i faced the following Problem i did this:


    total=2*(length[0]+length[1]+length[2]+length[3]+length[4]);
    total1=2*(length[0]+length[1]+length[2]+length[3]+length[4]+length[5]);

    int f= total1-total;

    for( int i=0; i<f; i++ )
    {
    for(int l=total;l<total1;l++)
    {


    text1=new JTextField(newString[l]);
    text1.setBounds(newPos[l],newPos[l+1],100,30);
    boolean editable = function[l].equals("true");
    text1.setEditable(editable);
    text1.setText("");
    text1.addFocusListener(this);

    panel.add(text1);

    System.out.println("total: "+total1);

    l++ ;

    }
    i++
    }



    However i cannot get the Array to stored in the value.. why??
    when i call out text1[0] it jumps to the last value , so does all the others like text[1],[2]....
     
  2. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    I realize English is not your first language, but it's hard for me to figure out what you're trying to do. What does this code fragment do? What is the result you were expecting? For one thing, it would be very helpful if you used variable names that actually represent the meaning of what they are supposed to hold. What are total, total1, f, i, l, and text1 supposed to mean? I can't tell by looking. Also, you should wrap your code in <leftbracket>code<rightbracket> tags when posting it, to preserve formatting (indentation, etc.).
     
  3. thread starter macrumors newbie

    Joined:
    Apr 24, 2006
    #3
    i Am Reading a XML and From there i did a Loop to display all the TextField but by adding 4 jTextfield will mess up what i have done Previously so i chose an array but i faced the following Problem
    this is wat i did:




    for( int i=0; i<4; i++ )
    {
    for(int l=63;l<67;l++) //this is the loop for the Display of Textfield as read(XML)
    {


    text1=new JTextField(newString[l]);
    text1.setBounds(newPos[l],newPos[l+1],100,30);
    boolean editable = function[l].equals("true");
    text1.setEditable(editable);
    text1.setText("");

    panel.add(text1);

    System.out.println("total: "+total1);

    l++ ;

    } .
    i++
    }



    However i cannot get the Array to stored in the value.. why??
    when i call out text1[0] it jumps to the last value , so does all the others like text[1],[2]....
     
  4. macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #4
    Woah, stop right there.

    1 st rule of code club: we are lazy bastards who hate debugging two problems at the same place.

    Since you seem to have problems with both your layout and your array handling, let's separate the two into methods.

    PHP:
    public void AddTextField(JPanel panestringlabelstring content){
      
    // label 
      
    JLabel l=new JLabel();
      
    l.setText(label);
      
    pane.Add(l);

     
    // textfield
     
    JTextField tf=new JTextField();
     
    tf.setText(content);
     
    pane.Add(tf);
    }

    public 
    void StartItAll(){
     
    contents=LoadXML() ; // todo: return a string[] from your xml
     
     
    JPanel panel=new JPanel(new GridLayout(2,0));  // 2 cols, n rows
     
    for(int i=0;i<contents.length;i++){
       
    AddTextField(pane,"textbox "+i,contents[i]); 
     } 
    }
    But.. the reason why text1[0] always gives the last result is because you do this :
    PHP:
    for( int i=0i<4i++ )
    {
    for(
    int l=63;l<67;l++) //this is the loop for the Display of Textfield as read(XML)
    {
    text1[i]=new JTextField(newString[l]); // text1[i] will be assigned 4 times, thus 3 of the 4 jTextFields are ignored... 
    If your code starts getting out of control, it's a sure sign you need to break it into functionnal subparts... good luck.
     
  5. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #5


    I'm going to stop you right here. This is not a proper way to sum elements of an array. You should use a for loop. Furthermore...

    This will always evaluate to 2*length[5] if you do the algebra. What are you actually trying to achieve here?
     
  6. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #6
    You have a nested loop,

    Code:
    for( int i=0; i<4; i++ ) {
        for(int l=63;l<67;l++) {
            text1[i]=new JTextField(newString[l]);
    
    That means you run through the outer loop four times and for every i, you also run through the inner loop four times. All in all you run through the inner section 16 times and create 16 textfields.

    You just want 4 different textfields, each stored with their own index in the text1 array, right?

    Change it to:
    Code:
        for(int l=63;l<67;l++) {
            int i = l-63;
            text1[i]=new JTextField(newString[l]);
    
    and you should be set.
     

Share This Page