For Loop

can anyone please change this to a for loop

JPanel tPanel = new JPanel(new GridBagLayout());
tPanel.setBackground(Color.magenta);

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;

tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
gbc.gridy = 0;
tPanel.add(new JLabel("TITLE"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("AUTHOR"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("PUBLISHER"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("YEAR"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("ISBN"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("CATEGORY"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("NUMBER"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("UNIT PRICE"),gbc);
gbc.gridx = 0;
gbc.gridy++;

LVL 1
lucinda77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hammadian2Commented:
dont what's your language
however
make an array of objects containning:
JTextField(20), JLabel("TITLE"), JTextField(20),JLabel("AUTHOR"),....

Then make ur for loop something like this:
for i = 1 to ArrayCount
      if (i mod 2) = 1 then gbc.gridx = 1 else gbc.gridx = 0
      gbc.gridy++;            
      tPanel.add(Objects[i],gbc);
Next


however, this kind of loop is really USELESS, no problem with repeating the code in such a case
0
adambuttCommented:
Since each lable has a different name, it really woulden't be worth it.  However, the text fields could be simplified to:

gbc.gridx = 0;
for(int i=0; i<8; i++)
{
gbc.gridy = i;
tPanel.add(new JTextField(20),gbc);
}

But, again, since you already have to put each lable on its own line, it isen't worth it.  You will save processor cycles just leaving it as it is.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SkonenCommented:
If you want to take care of gbc.gridx in the same loop use the modulus operator:


for(int i=0; i<8; i++) {
     gbc.gridx = (i+1) % 2;
     gbc.gridy = i;
     tPanel.add(new JTextField(20),gbc);
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.