Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 2, 2006, 09:47 PM   #1
Alvinkiang
macrumors newbie
 
Join Date: Apr 2006
Send a message via AIM to Alvinkiang Send a message via MSN to Alvinkiang
Storing Array

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]....
Alvinkiang is offline   0 Reply With Quote
Old May 2, 2006, 10:54 PM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: 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.).
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old May 3, 2006, 12:00 AM   #3
Alvinkiang
Thread Starter
macrumors newbie
 
Join Date: Apr 2006
Send a message via AIM to Alvinkiang Send a message via MSN to Alvinkiang
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]....
Alvinkiang is offline   0 Reply With Quote
Old May 3, 2006, 03:20 AM   #4
Palad1
macrumors 6502a
 
Palad1's Avatar
 
Join Date: Feb 2004
Location: 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 Code:
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 Code:
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.
Palad1 is offline   0 Reply With Quote
Old May 3, 2006, 07:00 PM   #5
savar
macrumors 68000
 
savar's Avatar
 
Join Date: Jun 2003
Location: District of Columbia
Send a message via AIM to savar
Quote:
Originally Posted by Alvinkiang
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...

Quote:
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?
__________________
Mehce
savar is offline   0 Reply With Quote
Old May 3, 2006, 07:33 PM   #6
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
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.
__________________
Someone get Nekkid™, now!
gekko513 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iTunes storing Tech198 Mac Applications and Mac App Store 3 Jan 3, 2014 09:29 PM
Storing Macbook Trey M MacBook Pro 4 Aug 9, 2013 10:07 PM
Storing documents Bedford61 MacBook Pro 1 Feb 26, 2013 04:27 PM
Storing in icloud Hachimenum iPad Tips, Help and Troubleshooting 2 Oct 19, 2012 07:34 PM
Storing only on my ipod. 3v0 iPod 3 Jul 11, 2012 06:19 AM

Forum Jump

All times are GMT -5. The time now is 03:57 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC