java, swing, Gridlayout problem

Posted by josh on Stack Overflow See other posts from Stack Overflow or by josh
Published on 2011-01-12T13:04:20Z Indexed on 2011/01/12 13:53 UTC
Read the original article Hit count: 381

Filed under:
|
|
|
|

I have a panel with GridLayout

But when I'm trying to run the program, only the first button out of 100 is shown. Futhermore, the rest appear only when I move the cursor over them. What's wrong with it?

Here's the whole class(Life.CELLS=10 and CellButton is a class which extends JButton)

public class MainLayout extends JFrame {

    public MainLayout() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(650, 750);
        setLayout(new FlowLayout());
        //setResizable(false);

        final JPanel gridPanel = new JPanel(new GridLayout(Life.CELLS, Life.CELLS));

        for (int i=0; i<Life.CELLS; i++) {
            for (int j=0; j<Life.CELLS; j++) {
                CellButton jb = new CellButton(i, j);
                jb.setPreferredSize(new Dimension(jb.getIcon().getIconHeight(), jb.getIcon().getIconWidth()));
                buttons[i][j] = jb;
                grid[i][j] = false;

                gridPanel.add(jb);
            }
        }
        add(gridPanel);
    }
}

This is code of CellButton

package classes;

import javax.swing.JButton; import javax.swing.ImageIcon; import javax.swing.JFrame;

public class CellButton extends JButton {

private int x;
private int y;
boolean alive;
ImageIcon icon;
boolean next;

// icons for grids
final ImageIcon dead = 
    new ImageIcon(JFrame.class.getResource("/images/image1.gif"));
final ImageIcon live = 
    new ImageIcon(JFrame.class.getResource("/images/image2.gif"));

public CellButton(int X, int Y) {
    super();
    x = X;
    y = Y;
    alive = false;
    icon = dead;
    setIcon(icon);
}   

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public boolean isAlive() {
    return alive;
}

public void relive() {
    alive = true;
    icon = live;
    setIcon(icon);
}

public void die() {
    alive = false;
    icon = dead;
    setIcon(icon);
}

public void setNext(boolean n) {
    next = n;
}

public boolean getNext() {
    return next;
}

public ImageIcon getIcon() {
    return icon;
}

}

© Stack Overflow or respective owner

Related posts about java

Related posts about swing