Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Alvinkiang

macrumors newbie
Original poster
Apr 24, 2006
14
0
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]....
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
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.).
 

Alvinkiang

macrumors newbie
Original poster
Apr 24, 2006
14
0
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]....
 

Palad1

macrumors 6502a
Feb 24, 2004
647
0
London, UK
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 pane, string, label, string 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=0; i<4; i++ )
{
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.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
Alvinkiang said:
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]);



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

int f= total1-total;

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

gekko513

macrumors 603
Oct 16, 2003
6,301
1
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.