Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:46:04+00:00 2026-06-16T01:46:04+00:00

I have a class Game and an array of buttons buts , i want

  • 0

I have a class Game and an array of buttons buts, i want to call function on the Game class fun(i,j) when any buts[i][j] is clicked , i tried like this:

buts = new JButton[Setting.length][Setting.width];
    Game game = new Game(setting);
    int hgap = 4, vgap = 4;
    panel = new JPanel(new GridLayout(Setting.length, Setting.width, hgap, vgap));
for (int i = 0; i < Setting.length; i++) {
            for (int j = 0; j < Setting.width; j++) {
                //JButton btn = new JButton();
                buts[i][j] = new JButton();
                buts[i][j].setText(String.valueOf(j));
                setColor(buts[i][j], Game.cells[i][j].getColor());
                buts[i][j].addActionListener(new java.awt.event.ActionListener() {

                    @Override
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        buttonActionPerformed(evt);
                    }
                });
                panel.add(buts[i][j]);
            }
        }
    private void buttonActionPerformed(ActionEvent evt) {

    }

but how can i send i,j to the function buttonActionPerformed ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:46:06+00:00Added an answer on June 16, 2026 at 1:46 am

    You could use a concrete Action for each JButton and pass in the values i & j into the constructor of the class.

    Adding:

    buts[i][j].setAction(new MyAction(i, j));
    

    The Action implementation:

    class MyAction extends AbstractAction {
       private final int iValue;
       private final int jValue;
    
       public MyAction(int i, int j) {
          this.iValue = i;
          this.jValue = j;
       }
    
       @Override    
       public void actionPerformed(ActionEvent e) {
          buttonActionPerformed(e, iValue, jValue);
       }
    }
    

    And:

    private void buttonActionPerformed(ActionEvent evt, int i, int j) {
       // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a game for class and for this game I have an array
I have a class called Mouse (tracking button states in a game). I want
So, i'm attempting simple card game. I have player class with draw function, and
I'm building a game, and for my players I have a player class. This
Currently I have an Array that is having 9 buttons pushed to it. This
I have classname, methodname and I have an array with parameters. I want to
I have a class with the following declaration of the fields: public class Game
Ok. I have a class Game, which creates an instance of my class Board,
I am making a card game in ruby. I have the Game class, which
Consider I have a game-creation framework, with a basic class called Game. // Action

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.