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 6913933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:16:55+00:00 2026-05-27T09:16:55+00:00

I have a JButton array and generate 100 buttons with for(i=0;i<button.length;i++) but can’t figure

  • 0

I have a JButton array and generate 100 buttons with

 for(i=0;i<button.length;i++)

but can’t figure out what to put as the e.getSource() ie if(e.getSource()==????){} what do I put in the ????. In other words, how do I find what a button created in an array is named in the case of e.getSource.

 import javax.swing.*;  
 import javax.swing.border.Border;
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.ArrayList;
 import java.awt.*;
 import java.awt.event.*;

 public class map {
     static int i;
     JFrame frame = new JFrame("D&D");
     public map() {
        int a=0,b=50;
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100,0,1000,600);
        frame.getContentPane().setLayout(null);      
        frame.setVisible(true);
        frame.setBackground(Color.black);
        frame.setResizable(false);
        for(i=0;i<button.length;i++){
            a=a+50;
            if(a>549) {
                b=b+50;
                a=50;
            }
            button[i]= new JButton(SD);
            frame.getContentPane().add(button[i]);
            button[i].setBounds(a, b, 50,50);
            button[i].setFont(new Font("Blackmoor Let", Font.BOLD, 30));
            button[i].setForeground(Color.red);
            button[i].setBorder(border);
            button[i].addActionListener(boardListener);
      }
    }

    ActionListener boardListener = new ActionListener (){
        public void actionPerformed(ActionEvent e){
            System.out.print("\n" +e.getSource());            
            if (e.getSource()==button[i]){              
                System.out.println("hi");
            }
        }
    };

    public static void main(String[]args){
         new map();
    }
}
  • 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-27T09:16:56+00:00Added an answer on May 27, 2026 at 9:16 am

    Use a List<JButton> rather than a JButton[] to hold your buttons, and use

     int index = listOfButtons.indexOf(e.getSource())
    

    to know the index of the clicked button.

    To create and populate the list:

    List<JButton> listOfButtons = new ArrayList<JButton>(100);
    for (int i = 0; i < 100; i++) {
        JButton button = ...;        
        listOfButtons.add(button);
    }
    

    If you want to convert an array of JButtons into a List<JButton> (but it’s not needed here), just use

    List<JButton> listOfButtons = Arrays.asList(buttons);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JButton array button[100] and use is in more than one method,
I have a jTable and a jButton. When clicked, the button's actionPerformed method calls
I have a JList, and a JButton, user can click an item in the
Let's say I have JButton test = new JButton(Test Button); and I want to
I have a 2D-Array of JButtons JButton[][] ledBtns = new JButton[8][8]; And in a
I have put few elements in array (e.g. 5 elements) The first element, index[0]
I have a custom button class called ImageButton that extends JButton. In it i
I have a parent JFrame it contain a JButton . functionality of that button
I have a JButton that's attached to an ActionListener, but I also wanted to
I have an array of JButtons which form a keypad interface. After six numbers

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.