I’m having trouble; I’m trying to implement a cinema booking system however I cannot set an actionListener to a specific button on different grids. The way I want it to work is each user will have a session in which he can pick seats and a ticket price such as student rate, old age pensionate rates. Anyway, I cannot seem to be able to add actionListener, so when he picks seats, those become unavailable while the program is running. Thank you.
// Load Libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class cinemaSystem {
// Global Variables
JFrame frame = new JFrame(); // Creates JFrame
JLabel title;
JButton l[][], m[][], r[][]; // Names grid of JButtons
JPanel panel1, panel2, panel3;
// Constructor
public cinemaSystem(){
title = new JLabel("Cinema Booking");
title.setFont(new Font("Helvetica", Font.BOLD, 30));
title.setLocation(12,5);
title.setSize(600, 60);
frame.setLayout(null); // Setting Grid Layout
// panel1.setLayout(new GridLayout(seat,row));
l = new JButton[4][4]; // Allocating Size of Grid
panel1 = new JPanel(new GridLayout(4,4));
panel1.setBackground(Color.black);
panel1.setBounds(20, 70, 200, 140);
for(int y = 0; y <4 ; y++){
for(int x = 0; x < 4; x++){
l[x][y] = new JButton("L" + y + x); // Creates New JButton
// l[x][y].addActionListener(this);
panel1.add(l[x][y]); //adds button to grid
}
}
m = new JButton[4][2]; // Allocating Size of Grid
panel2 = new JPanel(new GridLayout(2,4));
panel2.setBackground(Color.black);
panel2.setBounds(240, 140, 200, 70);
for(int y = 0; y <2 ; y++){
for(int x = 0; x < 4; x++){
m[x][y] = new JButton("M" + y + x); // Creates New JButton
panel2.add(m[x][y]); //adds button to grid
}
}
r = new JButton[4][4]; // Allocating Size of Grid
panel3 = new JPanel(new GridLayout(4,4));
panel3.setBackground(Color.black);
panel3.setBounds(460, 70, 200, 140);
for(int y = 0; y <4 ; y++){
for(int x = 0; x < 4; x++){
r[x][y] = new JButton("R" + y + x); // Creates New JButton
panel3.add(r[x][y]); //adds button to grid
}
}
frame.add(title);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.setPreferredSize(new Dimension(680, 280));
frame.setTitle("Cinema Booking");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
// Main Class
public static void main(String[] args) {
new cinemaSystem(); //makes new ButtonGrid with 2 parameters
}
}
You can get the text held by the pressed JButton simply by calling
getActionCommand()on the ActionEvent object passed into theactionPerformed(...)method, or you can get which button was pressed via itsgetSource()method. Either of these can help you find the JButton in your arrays and inactivate it.Other suggestions:
thisas your ActionListener. Instead consider using an anonymous inner class or private inner class. This will make your code much more flexible and your ActionListeners much simpler.For example this ActionListener will show which button was pressed and will inactivate that button:
Which can be added to the JButtons in a fashion similar to this…
Plus using appropriate layout managers produced a GUI that looked like this:
