Bookmark Topic Watch Topic
  • New Topic

How to add action listeners to grid buttons?

 
chanakya nani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Report post to moderator
Here's my code...

I want to add Action Listeners to each and every button in the grid...Can you help me please..



 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
In pseudo code

Run in a loop
Create a button instance.
Add a listener to the button*
Add the button to the parent container

*Adding the listener can be done in multiple ways. Let some class implement the ActionListener or use anonymous inner class or subclass AbstractAction and share it with all the buttons. How you do it would be more dependent on your requirement. Only scenario I can imagine where you would require 64 buttons in a grid is a game of chess. Unless of course this is some academic exercise.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
This question would be more suitable on the UI forums. Moving thread.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
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:

 
chanakya nani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Report post to moderator
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:





Thank you very much....How could i miss that simple thing?..!!..I feel i am such a douche..
Thank you once again.
Ciao.
 
Martijn van der Geest
Greenhorn
Posts: 5
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Or you could simply create the button based on an action, which has a name and/or icon and offcourse the action.
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
This topic appears to be continued here:
http://www.coderanch.com/t/523997/GUI/java/do-access-particular-button-grid
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
chanakya, please UseOneThreadPerQuestion. I'll close this one since you obviously prefer the newer thread.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!