Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to add action listeners to grid buttons?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..



 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This question would be more suitable on the UI forums. Moving thread.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This topic appears to be continued here:
https://coderanch.com/t/523997/GUI/java/do-access-particular-button-grid
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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