Hi, I am having a hard time trying to figure out how to have my JButtons go through an Array to extract information using MouseListener. I have put a link to my assignment, I'm only having trouble with part 4 of the assignment: http://http://people.scs.carleton.ca...6_A7_W2014.pdf
I've attached a zip file with all of my code in it as well. Thanks
Here is the code for my StadiumPanel Class
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class StadiumPanel extends BoardPanel implements MouseListener{ private int row; private int column; public StadiumPanel(Stadium s){ setLayout(new GridLayout(s.ROWS, s.COLUMNS,0,0)); setBackground(Color.white); for(int r = 0; r < s.ROWS; r++){ for(int c = 0; c < s.COLUMNS; c++){ if(s.getSeat(r,c) == null){ JLabel nullLabel = new JLabel(""); add(nullLabel); }else{ if(s.getSeat(r,c).getSection() == 1){ JButton sec1Button = new JButton(""); sec1Button.setBackground(Color.red); sec1Button.addMouseListener(this); add(sec1Button); } else if(s.getSeat(r,c).getSection() == 2){ JButton sec2Button = new JButton(""); sec2Button.setBackground(Color.green); sec2Button.addMouseListener(this); add(sec2Button); } else if(s.getSeat(r,c).getSection() == 3){ JButton sec3Button = new JButton(""); sec3Button.setBackground(Color.blue); sec3Button.addMouseListener(this); add(sec3Button); }else if(s.getSeat(r,c).getSection() == 4){ JButton sec4Button = new JButton(""); sec4Button.setBackground(Color.yellow); sec4Button.addMouseListener(this); add(sec4Button); } } } } } public void mouseEntered(MouseEvent e){ System.out.println("Entered"); System.out.println("getSource(): " + e.getSource()); } public void mouseExited(MouseEvent e){ System.out.println("Exited"); } public void mousePressed(MouseEvent e){ System.out.println("Pressed"); } public void mouseClicked(MouseEvent e){ System.out.println("clicked"); } public void mouseReleased(MouseEvent e){ System.out.println("Released"); } private void seatButton(int r, int c){ row = r; column = c; } public static void main(String args[]) { JFrame f = new JFrame("Stadium Panel Test"); f.getContentPane().add(new StadiumPanel(new Stadium())); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(700, 540); f.setVisible(true); } }
import java.awt.*; import javax.swing.*; public class StadiumApp extends JFrame{ public StadiumApp(String title){ super(title); GridBagLayout layout = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); getContentPane().setLayout(layout); StadiumPanel stadiumPanel = new StadiumPanel(new Stadium()); constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = 1; constraints.gridheight = 4; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTHWEST; constraints.ipadx = 0; constraints.ipady = 0; constraints.insets = new Insets(0,0,0,0); constraints.weightx = 0; constraints.weighty = 0; layout.setConstraints(stadiumPanel, constraints); getContentPane().add(stadiumPanel); SeatInfoPanel seatInfo = new SeatInfoPanel(); constraints.gridx = 1; constraints.gridy = 0; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTHWEST; constraints.ipadx = 0; constraints.ipady = 0; constraints.insets = new Insets(0,0,0,0); constraints.weightx = 0; constraints.weighty = 0; layout.setConstraints(seatInfo, constraints); getContentPane().add(seatInfo); SelSeatPricing pricing = new SelSeatPricing(); constraints.gridx = 1; constraints.gridy = 1; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTHWEST; constraints.ipadx = 0; constraints.ipady = 0; constraints.insets = new Insets(0,0,0,0); constraints.weightx = 0; constraints.weighty = 0; layout.setConstraints(pricing, constraints); getContentPane().add(pricing); JButton purchase = new JButton("Purchase"); constraints.gridx = 1; constraints.gridy = 2; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTHWEST; constraints.ipadx = 0; constraints.ipady = 0; constraints.insets = new Insets(0,0,0,0); constraints.weightx = 0; constraints.weighty = 0; layout.setConstraints(purchase, constraints); getContentPane().add(purchase); JButton admin = new JButton("Administrator"); constraints.gridx = 1; constraints.gridy = 3; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTHWEST; constraints.ipadx = 0; constraints.ipady = 0; constraints.insets = new Insets(0,0,0,0); constraints.weightx = 0; constraints.weighty = 0; layout.setConstraints(admin, constraints); getContentPane().add(admin); setResizable(false); setSize(800,500); } public static void main(String[] args) { new StadiumApp("Seat Purchase System").setVisible(true); } }
import java.awt.*; import javax.swing.*; public class SelSeatPricing extends JPanel{ public SelSeatPricing(){ setBorder(BorderFactory.createTitledBorder("Selected Seat Pricing")); setLayout(new GridLayout(3, 2,5,5)); JLabel seatPrice = new JLabel("Seat(s) Price:"); add(seatPrice); JTextField seatPriceText = new JTextField(); seatPriceText.setSize(70, 30); add(seatPriceText); JLabel hst = new JLabel("HST:"); add(hst); JTextField hstText = new JTextField(); hstText.setSize(70, 30); add(hstText); JLabel totalCost = new JLabel("Total Cost:"); add(totalCost); JTextField costText = new JTextField(); costText.setSize(70, 30); add(costText); } }
import java.awt.*; import javax.swing.*; public class SeatInfoPanel extends JPanel{ public SeatInfoPanel(){ setBorder(BorderFactory.createTitledBorder("Seat Information")); setLayout(new GridLayout(4, 2,5,5)); JLabel section = new JLabel("Section:"); add(section); JTextField sectionText = new JTextField(); sectionText.setSize(70, 30); add(sectionText); JLabel row = new JLabel("Row:"); add(row); JTextField rowText = new JTextField(); rowText.setSize(70, 30); add(rowText); JLabel number = new JLabel("Number:"); add(number); JTextField numberText = new JTextField(); numberText.setSize(70, 30); add(numberText); JLabel price = new JLabel("Price:"); add(price); JTextField priceText = new JTextField(); priceText.setSize(70, 30); add(priceText); } }