Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Link JButton to Array so MouseListener can extract information

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Link JButton to Array so MouseListener can extract information

    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);
     
     
      }
     
     
    }
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Link JButton to Array so MouseListener can extract information

    how to have my JButtons
    Are you asking how to write an action listener to respond to presses to a button?
    See the tutorial: How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    If not that, please explain.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to get information of jtextfield by using a jbutton in a loop
    By justinbieber1372 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 28th, 2014, 05:51 AM
  2. How to use threads to extract information
    By Stark20 in forum Threads
    Replies: 1
    Last Post: December 8th, 2013, 10:42 PM
  3. suggestions required on Jlabel(ImageIcon) and Jbutton for mouselistener
    By syedfahadjalali in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2013, 02:12 AM
  4. How to interact with an application and extract information from that?
    By Karthik_P in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 8th, 2013, 02:44 AM
  5. Getting information from an array
    By andresfelquintero in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 3rd, 2011, 04:53 PM