How should i handle click events of a JButton array
i have frame containing 100 JButtons .I need to find out which one was pressed by the user

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class Screen1 implements ActionListener , WindowListener
{ 
	int i;
	JFrame scrfrm1;
	JLabel lblscr;
	JButton seats[] = new JButton[100];
	int left,top;
	Screen1()
	{
		scrfrm1 = new JFrame("Screen1");
		
		for(i=0;i<100;i++)
		seats[i] = new JButton(String.valueOf(i + 1));
		//line one of buttons
		//seat1.setBounds
		//setting cutomised button layout
		scrfrm1.addWindowListener(this);
		scrfrm1.setLayout(null);
		scrfrm1.setBounds(100,0,1200,740);
		scrfrm1.setVisible(true);
		scrfrm1.setResizable(true);
	}
	public void actionPerformed(ActionEvent ae)
	{
		for(i=0;i<100;i++)
		{
				if(ae.getSource()==seats[i])	
				{
					
					
				}
		}
		
	}
	public void windowOpened(WindowEvent we){}
	public void windowClosed(WindowEvent we){}
	public void windowClosing(WindowEvent we)
	{
		/*String ObjButtons[] = {"Yes","No"};
		int PromptResult = JOptionPane.showOptionDialog(null,"Are you sure you want to exit?","Multiplex Theatre Central",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,ObjButtons,ObjButtons[1]);*/
		//if(PromptResult==0)
		//{
			System.exit(0);
		//}
		//else
		//{
			//MainPage mp=new MainPage();
		//}
	}
	public void windowIconified(WindowEvent w){}
	public void windowDeiconified(WindowEvent w){}
	public void windowActivated(WindowEvent w){}
	public void windowDeactivated(WindowEvent w){}
	public static void main(String args[])
	{
		Screen1 scr1 = new Screen1();
	}
}
seats[i] = new JButton(String.valueOf(i + 1));
seats[i].addActionListener(this);

public void actionPerformed(ActionEvent ae){
    String text = ((JButton) e.getSource()).getText();
           //..
}

you don't need loop inside actionPerformed method

Edited 3 Years Ago by Dani: Formatting fixed

quuba's suggestion is OK, it will work, but it ties the external representation of the button text to the internal functions of the code, which is not ideal as a programming technique. (What happens when you do the Japanese version?)
If you want to do this the "professional" way, all JComponents support a "Client Property" collection where you can store anything you want as a property if the component:

seats[i].putClientProperty("index", new Integer(i));
...
public void actionPerformed(ActionEvent ae){
JButton theButton = (JButton) ae.getSource();
int index = (Integer) theButton.getClientProperty("index");

What's so good about this is that you can have any number of properties, and they can be strings, URLs, Files, anything that you want to associate with the button.

Comments
It's good to learn new things

[TEX][/TEX]please guys help me to create an array using jbuttons .
the buttons should be labeled as find and insert.

This question has already been answered. Start a new discussion instead.