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

  • Home
  • SEARCH
  • 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 3786160
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:38:22+00:00 2026-05-19T11:38:22+00:00

I am using a for loop to create a grid of 60 JButtons. What

  • 0

I am using a for loop to create a grid of 60 JButtons. What I want to do is have the name of the actionListener increment each time the loops runs:

for (int y = 0; y < game.getHeight(); y++) {
        for (int x = 0; x < game.getWidth(); x++) {
            JButton square = new JButton();
            square.setFont(new Font("Verdana", Font.PLAIN, 20));
            ActionListener bh = new button_handler();
            square.addActionListener(bh);
            grid.add(square);
        }

So I would like the name to increment (bh_1, bh_2, bh_3, etc).

Thanks!

  • 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-05-19T11:38:23+00:00Added an answer on May 19, 2026 at 11:38 am

    If your goal is to be able to keep track of the ActionListeners by name after creation, then you will want to use an array to do this. (If this is not why you want to name them differently, then I will need some more info.)

    Assume you have an ActionListener[60] called actionListeners and a counter called buttonCount.

    JButton square = new JButton();
    square.setFont(new Font("Verdana", Font.PLAIN, 20));
    ActionListener bh = new button_handler();
    actionListeners[buttonCount++] = bh; // store the handler in an array for later
    square.addActionListener(bh);
    grid.add(square);
    

    Now you will be able to access the ActionListeners from the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a loop that reads each line in a file using getline() :
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I just want a quick way (and preferably not using a while loop)of createing
How can I iterate over each file in a directory using a for loop?
I'm using the following syntax to loop through a list collection: For Each PropertyActor
I have a 2-dimensional array of objects and I basically want to databind each
I'm using the jQuery audio player 'jPlayer', I have a loop which creates a
using loop, I can create My cat is: Cat1 ... My cat is: Cat1
I'm pulling an xml and using a for loop to create a thumb list.
I have a large NSDictionary that I need to loop through and create separate

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.