0

all I have small problem with this piece of code. I'd say it's something small. I want to add a value to the arrayList everytime I press the button. What is happening now when i press the button it only saves the last value that has been clicked. How do I make it to save all the values?

Here is the piece of code...

public void actionPerformed(ActionEvent e) {

            seatArr = new ArrayList<Integer>();
            int a;

            if(e.getSource() instanceof JButton){

                bookedIcon = new ImageIcon("images/bookedSeat.png");

                a = Integer.parseInt(e.getActionCommand());
                seat = a;
                a =a- 1;
                seatsA[a].setIcon(bookedIcon);

                    seatArr.add(seat);
                try {
                    stmt.executeUpdate("UPDATE seats SET type = 'booked' WHERE seatNum ="+seat);
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

    }

I have no idea what might be the problem, it looks okay to me... Here is the rest of the code....

//start of  class------------------------------------------------
public class SeatMap extends JFrame implements ActionListener{

    static Connection conn;
    static Statement stmt, stmt1;
    static ResultSet rs, rs1;
    JComboBox <Integer> adultCombo;
    JComboBox <String> childCombo;
    String[] adultArray = {"0", "1", "2", "3"};
    String[] childArray = {"0", "1", "2"};
    JLabel childLabel;
    Icon bookedIcon;
    JButton nextButton, previousButton;
    public static ArrayList<Integer> seatArr;

    int seat = 0;
    int counter;
    JButton[] seatsA = new JButton[30];


//start of main--------------------------------------------------
    public static void main(String [] args){
        JFrame frame = new SeatMap();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//closes the program when x is pressed
    }
//start of frame------------------------------------------------
    public SeatMap(){
        super("Pick a Seat");

        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql://localhost/airlines","root", "");
            //Create a statement 
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT seatNum, type FROM seats");



        //---------------------------------------------------
        JPanel comboPanel = new JPanel();
        JLabel adultLabel = new JLabel("Adult Tickets:  ");
        childLabel = new JLabel("Child Ticket:  ");
        childLabel.setAlignmentX(LEFT_ALIGNMENT);
        childLabel.setVisible(false);

        adultCombo = new <String>JComboBox(adultArray);
        adultCombo.addActionListener(this);

        childCombo = new <String>JComboBox(childArray);
        childCombo.setVisible(false);

        comboPanel.add(adultLabel);
        comboPanel.add(adultCombo);
        comboPanel.add(childLabel);
        comboPanel.add(childCombo);
        Icon itbIcon = new ImageIcon("images/freeSeat.png");

        //setting center panel----------------------------------------------
            JPanel centerPanel = new JPanel();
            centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
            centerPanel.add(comboPanel);
        //setting label for seats------------------------------------------------
            JLabel label1 = new JLabel("Pick a seat: ", SwingConstants.CENTER);
            label1.setFont(new Font("Sans-serif", Font.PLAIN, 25));
            label1.setAlignmentX(CENTER_ALIGNMENT);
            centerPanel.add(label1);

            JPanel panel1 = new JPanel(new GridLayout(10,3));


        //for loop to print seats------------------------------------------------------------------------
            while(rs.next()){
                //System.out.println(rs.getString("seatNum"));

                int c = Integer.parseInt(rs.getString("seatNum"));
                 c = c - 1;

                seatsA[c] = new JButton(String.valueOf(c + 1));

                seatsA[c].setBackground(Color.WHITE);

                if(rs.getString("type").equals("free")){
                    seatsA[c].setIcon(itbIcon);
                    seatsA[c].addActionListener(this);
                }

                else if(rs.getString("type").equals("booked")){
                    bookedIcon = new ImageIcon("images/bookedSeat.png");
                    seatsA[c].setIcon(bookedIcon);
                    seatsA[c].setEnabled(false);
                }

                panel1.add(seatsA[c]);


            //}
            panel1.setAlignmentX(CENTER_ALIGNMENT);
            centerPanel.add(panel1);
            }
            //adding buttons---------------------------------------------

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
            buttonPanel.setAlignmentX(CENTER_ALIGNMENT);

            previousButton = new JButton("Previous");
            previousButton.setBackground(new Color(102,200,1));
            previousButton.setFont(new Font("Sans-serif", Font.PLAIN, 20));
            previousButton.setAlignmentX(JButton.CENTER_ALIGNMENT);
            previousButton.addActionListener(new ActionListener() { 
                  public void actionPerformed(ActionEvent e) { 
                        dispose();
                        try {
                            rs.close();
                            stmt.close();
                            conn.close();
                        } catch (SQLException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                        seatArr.clear();
                        JFrame form = new Form();

                    }} );
            buttonPanel.add(previousButton);

            nextButton = new JButton("Next");
            nextButton.setBackground(new Color(102,200,1));
            nextButton.setFont(new Font("Sans-serif", Font.PLAIN, 20));
            nextButton.setAlignmentX(JButton.CENTER_ALIGNMENT);
            nextButton.addActionListener(new ActionListener() { 
                  public void actionPerformed(ActionEvent e) { 
                        try {

                            String resNum = Form.getReservationNum();
                            System.out.println(seatArr);
                            stmt.executeUpdate("UPDATE users SET seats='"+seatArr+"' WHERE reservationNum ='"+resNum+"'");

                            seatArr.clear();
                            dispose();

                            rs.close();
                            stmt.close();
                            conn.close();
                        } catch (SQLException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        JFrame ticket = new Ticket();
                      } 
                    } );


            buttonPanel.add(nextButton);

            centerPanel.add(buttonPanel);
            //-----------------------------------------------------------

    //building header and footer-------------------------------------------


            JPanel header = new JPanel();
            HeadAndFooter.head(header);
            JPanel footer = new JPanel();
            HeadAndFooter.footer(footer);


    //adding panels to the content pane------------------------------------
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BorderLayout());
            mainPanel.add(header, BorderLayout.NORTH);
            mainPanel.add(centerPanel, BorderLayout.CENTER);
            mainPanel.add(footer, BorderLayout.SOUTH);
            getContentPane().add(mainPanel);//adding panels to the frame
        //----------------------------------------------------------------------------------------------
        setVisible(true);
        setSize(700,650);



        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

public void actionPerformed(ActionEvent e) {

            seatArr = new ArrayList<Integer>();
            int a;

            if(e.getSource() instanceof JButton){

                bookedIcon = new ImageIcon("images/bookedSeat.png");

                a = Integer.parseInt(e.getActionCommand());
                seat = a;
                a =a- 1;
                seatsA[a].setIcon(bookedIcon);

                    seatArr.add(seat);
                try {
                    stmt.executeUpdate("UPDATE seats SET type = 'booked' WHERE seatNum ="+seat);
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

        String choice = adultCombo.getSelectedItem().toString();

        if(choice.equals("0")){
            childCombo.setVisible(false);
            childLabel.setVisible(false);   
        }

        else{
            childCombo.setVisible(true);
            childLabel.setVisible(true);
        }
    }

}

1 Answer 1

1

seatArr = new ArrayList();

The problem is you are initializing the seatArr list everytime you performed the action, so basically it resets the list and add the latest value to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.