This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I access a particular button in a grid.?  RSS feed

 
chanakya nani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
I 'm stuck in middle of the code....How do i access a particular button in a grid(8/8) as accessing an element in an array like


My entire code(or atleast upto what i have written) is below...for a quick reference



 
Paul Clapham
Sheriff
Posts: 22503
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
chanakya nani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



i want each button to display some text when i click it...as i can't write ActionListener and Performed methods for each and everyone of the 64 buttons i need a loop to perform that....So i created a loop which creates all the 64 buttons and listeners...but when i run it i am getting the output, that is ,the text is printed only on the last button even if i click any button out of 64 ones...

once if you run the code you may understand perfectly what my problem is..
 
chanakya nani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



Actually the question i'm trying to do has conditions:


1. Game of Life is a one player game invented by John Conway. The game is played on a 8*8
two-dimensional grid of square cells. At any given time, a cell is either living or dead. Patterns of cells
transform themselves according to a simple set of rules:
• If a cell is living, it continues to live if surrounded by exactly two or three living cells. If it is
surrounded by more than three living cells, it dies of overcrowding; if less than two of its neighbors is
alive, it dies of loneliness.
• If a cell is dead, it becomes living only if it is surrounded by exactly three living cells.
Otherwise, it remains dead.
Simulate the game of Life program. Assume an initial configuration which can be specified by the
user and the program should start from that starting configuration.
 
chanakya nani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



My idea is if i click the button it should check the conditions and display "live" or "dead"..
 
Paul Clapham
Sheriff
Posts: 22503
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chanakya nani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



Yeah,My design is that the controller should be able to identify the state of the button...how do i do that and what's wrong in my code..can you please tell me..?

hey,But how does the game work if user is not interacting...the question says that i should take input from the user..??
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This appears to be continued from here:
http://www.coderanch.com/t/523960/GUI/java/add-action-listeners-grid-buttons
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!