Matthew Brown wrote:So how do you expect an icon to be visible on the button, when you are explicitly setting that icon to null? What do you think the createImage() method is doing?
Matthew Brown wrote:I may be missing something obvious...but in the code you've just posted, createImage returns null. Which would explain why you can't see any image - there isn't one to see. Is that the correct code?
Paul Clapham wrote:It doesn't make sense to say "the" button because you have 64 buttons, not just one.
But let's suppose you aren't seeing an icon on any of those 64 buttons. (Your description isn't very clear so it could mean other things, but let's start with that as a first guess.) Then that would mean your createImage() method is returning something which can't be seen by you.
There are other possibilities, like for example perhaps you aren't calling pack() or setVisible(true) on the JFrame at the right time... in fact I see you call setVisible(true) before you add the buttons; that might be the problem too. It's better to use a standard set of code (see the Swing tutorial for an example of a simple Swing program) rather than throwing together code at random.
Matthew Brown wrote:So what's happening at the moment? Don't ask us to guess what the problem is. TellTheDetails.
Paul Clapham wrote:Ah, I see. So your problem isn't really "accessing" the buttons. Your problem is that each button should be able to identify its neighbours, and should be able to identify the state (dead or alive) of those neighbours.
Or perhaps your design should be that the controller should be able to identify a button's neighbours, and should be able to identify the state of a button.
So which is your design? Is it the controller which will be doing the work, or the buttons themselves? I can't tell from your code; remember that designing should be done before coding.
More fundamentally: I don't understand why you have buttons at all. The Game Of Life doesn't involve a user interacting with the grid cells at all, as far as I can see.
Paul Clapham wrote:If you want a 2-dimensional array of buttons, it wouldn't be hard to modify that code to produce one. But why do you want to "access" a button in a grid? What does it mean to "access" a button?
Paul Clapham wrote:If you want a 2-dimensional array of buttons, it wouldn't be hard to modify that code to produce one. But why do you want to "access" a button in a grid? What does it mean to "access" a button?
Paul Clapham wrote:If you want a 2-dimensional array of buttons, it wouldn't be hard to modify that code to produce one. But why do you want to "access" a button in a grid? What does it mean to "access" a button?
Jesper de Jong wrote:Do you already know how to add an action listener to a single button? If you know that, it's easy to do the same thing in a loop.
First of all, I'd split line 13 in two parts: creating the button and adding it to the frame, because you need a reference to the JButton object so that you can add an action listener to it: