0

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();
	}
}
4
Contributors
3
Replies
7
Views
7 Years
Discussion Span
Last Post by n88y
Featured Replies
  • 2

    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 Read More

  • 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 … Read More

2
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 by Dani: Formatting fixed

2

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.

Votes + Comments
It's good to learn new things
0

[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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.