tags:

views:

48

answers:

1

I am trying to add a JButton Array to my GridLayout but it seems not to be working.

Probably a rookie mistake -- what am I doing wrong?

import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
public class Grid extends JApplet
{
    public JButton[] inv;
    public void init()
    {
        setLayout(new GridLayout(4,5));
        int i = 0;
        while(i>20)
        {
            inv[i] = new JButton("Slot #" + i);
            add(inv[i]);
            System.out.println("Button " + i + " added.");
            i++;
        }
    }
}

PS - If you something that could be programmed in a better manner -- please fix it for me.

Thank you.

A: 
  1. You didn't create the array, read your text books on how to allocate entries for an array. The Learning the Java Language tutorial has a section on using arrays.

  2. Your loop is wrong. Is i ever greater than 20?

camickr
You're no help. But the way you're talking down to me makes me look like an idiot. Get some kindness in your heart.
Dan
I got it fixed.
Dan
Not only did I solve your immediate problem with the loop, I pointed out your next problem with the null array. I don't believe in spoon feeding answers, you need to learn to develop your own problem solving skills. Also, you need to learn to read tutorials (which is why I gave you the link) and the API.
camickr