1
import java.awt.*;

import javax.swing.*;
import java.util.Timer;

public class GUI extends JPanel
{


    private static final long serialVersionUID = 1L;
    private static final int ROWS = 50;
    private static final int COLS = 30;
    public JButton[][] buttons = new JButton[ROWS][COLS];



    public GUI()
    {
        setLayout(new GridLayout(ROWS, COLS, 1,1));
        for (int row = 0; row < buttons.length; row++) {
             for (int col = 0; col < buttons[row].length; col++) {
                JButton button = new JButton("");
                add(button);
                buttons[row][col] = button;
                }
             }
              buttons[0][0].setBackground(Color.BLACK);



    }
    public void  start()
    {
            GUI gui = new GUI();

              JFrame frame = new JFrame("The Game Of Life");
              frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              frame.getContentPane().add(gui);
              frame.pack();
              frame.setLocationByPlatform(true);
              frame.setVisible(true);  


    }
    public void run() 
    {
        this.start();       
        this.setColor('g', 0, 0);





    }
    public void setColor(char c, int i, int j)
    {
        setLayout(new GridLayout(ROWS, COLS, 1,1));

        for (int row = 0; row < buttons.length; row++) {
             for (int col = 0; col < buttons[row].length; col++) {
                JButton button = new JButton("");
                add(button);
                buttons[row][col] = button;

        switch(c){
        case 'g':
            buttons[i][j].setBackground(Color.GREEN);
        case 'r':
            buttons[i][j].setBackground(Color.RED);
        case 'b':
            buttons[i][j].setBackground(Color.BLACK);
             }
        }
        }
    }
}   

I can get the "buttons[0][0]" to change color but nowhere else has seemed to work (the button[0][0] is there as an example that its the only place where it will change the button color) the class GUI is part of a system that is supposed to create the biology "game of life" this is the code for the Gui interface I need the code to create a grid of 50x30 buttons and I will end up making each button change color according to the attributes of the other buttons around it. The biggest problem at this point is I cant get the buttons to change color at all unless its done in the GUI constructor. if anybody can help me I would be incredibly thankful this is part of a CS project. If you think you need more info I'm happy to post whatever is needed just ask. P.S. I realize the code may be a little messy I have been fiddling with it so I apologize.

import java.awt.*;
import javax.swing.*;


public class View extends JPanel
{
    private static final long serialVersionUID = 1L;
    private static final int ROWS = 50;
    private static final int COLS = 30;
    GUI gui = new GUI();

    JFrame frame;

    public View()
   {

}

public void run()
{

    //timer.start();
    gui.run();


}   

public void setButtons(Cell[][] colorset)
{
    for(int i = 0; i<ROWS; i++)
    {
        for(int j = 0; j<COLS; j++)
        {
            switch(colorset[i][j].getCurrent())
            {
            case 0:
                gui.setColor('g', i, j);
            case 1:
                gui.setColor('r', i, j);
            case 2:
                gui.setColor('b', i, j);
            default:

            }
        }
    }
    //this.run();
}

}'

Carl5444
  • 25
  • 4
  • 2
    Please put a little more work into the question. For instance, you haven't told us just what the code is supposed to do! Also, please go through the [tour], the [help] and the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) sections to see how this site works and to help you improve your current and future questions, which can help you get better answers. – Hovercraft Full Of Eels Oct 02 '15 at 01:25
  • 1
    Only as opposed to what? You can't change the button background colour anywhere else? Or you can't change anything else in the GUI constructor? – user253751 Oct 02 '15 at 01:32
  • I cant change the button background color anywhere else – Carl5444 Oct 02 '15 at 01:43
  • 1
    I don't see you trying to set it anywhere else. – Hovercraft Full Of Eels Oct 02 '15 at 01:44
  • If I call setColor should it not change the button color? – Carl5444 Oct 02 '15 at 01:50
  • But you call setColor once and only once, and it's not going to magically call itself. Try calling it inside of some event such as an button's ActionListener. – Hovercraft Full Of Eels Oct 02 '15 at 01:51
  • I run the code through a loop in another part of the program ill add the other class to the code now – Carl5444 Oct 02 '15 at 01:53

1 Answers1

2

Your problem is that you want to have setColor(...) called while the program is running. The solution is to call it in some event code, and what code will depend on what event you want to trigger its call. If it's a JButton press, then place it inside of a JButton's ActionListener. If you want it called every xxx mSec's, then place it inside of the ActionListener of a Swing Timer. If you want it to respond to key-press, then place it inside of a Key Bindings Action.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373