PDA

View Full Version : Storing Array




Alvinkiang
May 2, 2006, 09:47 PM
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[i]=new JTextField(newString[l]);
text1.setBounds(newPos[l],newPos[l+1],100,30);
boolean editable = function[l].equals("true");
text1[i].setEditable(editable);
text1[i].setText("");
text1[i].addFocusListener(this);

panel.add(text1[i]);

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
May 2, 2006, 10:54 PM
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
May 3, 2006, 12:00 AM
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[i]=new JTextField(newString[l]);
text1.setBounds(newPos[l],newPos[l+1],100,30);
boolean editable = function[l].equals("true");
text1[i].setEditable(editable);
text1[i].setText("");

panel.add(text1[i]);

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
May 3, 2006, 03:20 AM
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.


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 :

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
May 3, 2006, 07:00 PM
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
May 3, 2006, 07:33 PM
You have a nested loop,


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:

for(int l=63;l<67;l++) {
int i = l-63;
text1[i]=new JTextField(newString[l]);

and you should be set.