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

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.

commented: It's good to learn new things +4

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.