Question: Java Code....Update button does not work,what is the issue? import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import javax.swing.JButton;

Java Code....Update button does not work,what is the issue?

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel;

public class CinemaManagementSystem extends JFrame implements ActionListener {

// Create labels for each of the fields JLabel lblMovieName, lblShowTime, lblMoviePrice, lblNumSeats, lblPriceTotal;

// Create text fields for each of the fields JTextField txtMovieName, txtShowTime, txtMoviePrice, txtNumSeats, txtPriceTotal;

// Create buttons for add, update, delete, and print JButton btnAdd, btnUpdate, btnDelete, btnPrint;

// Create a table to display the records JTable tblRecords; DefaultTableModel model;

// Create an array list to hold the records ArrayList records = new ArrayList<>();

public CinemaManagementSystem() { // Set the title of the JFrame setTitle("Cinema Booking System");

// Set the size of the JFrame setSize(800, 600);

// Set the layout to null setLayout(null);

// Create labels for each of the fields lblMovieName = new JLabel("Movie Name:"); lblMovieName.setBounds(20, 20, 100, 30); add(lblMovieName);

lblShowTime = new JLabel("Show Time:"); lblShowTime.setBounds(20, 60, 100, 30); add(lblShowTime);

lblMoviePrice = new JLabel("Ticket Price:"); lblMoviePrice.setBounds(20, 100, 100, 30); add(lblMoviePrice);

lblNumSeats = new JLabel("Number of Seats:"); lblNumSeats.setBounds(20, 140, 100, 30); add(lblNumSeats);

lblPriceTotal = new JLabel("Total Price:"); lblPriceTotal.setBounds(20, 180, 100, 30); add(lblPriceTotal);

// Create text fields for each of the fields txtMovieName = new JTextField(); txtMovieName.setBounds(120, 20, 200, 30); add(txtMovieName);

txtShowTime = new JTextField(); txtShowTime.setBounds(120, 60, 200, 30); add(txtShowTime);

txtMoviePrice = new JTextField(); txtMoviePrice.setBounds(120, 100, 200, 30); add(txtMoviePrice);

txtNumSeats = new JTextField(); txtNumSeats.setBounds(120, 140, 200, 30); add(txtNumSeats);

txtPriceTotal = new JTextField(); txtPriceTotal.setBounds(120, 180, 200, 30); add(txtPriceTotal);

// Create buttons for add, update, delete, and print btnAdd = new JButton("Add"); btnAdd.setBounds(120, 220, 80, 30); btnAdd.addActionListener(this); add(btnAdd);

btnUpdate = new JButton("Update"); btnUpdate.setBounds(220, 220, 80, 30); btnUpdate.addActionListener(this); add(btnUpdate); btnDelete = new JButton("Delete"); btnDelete.setBounds(320, 220, 80, 30); btnDelete.addActionListener(this); add(btnDelete);

btnPrint = new JButton("Print"); btnPrint.setBounds(420, 220, 80, 30); btnPrint.addActionListener(this); add(btnPrint);

// Create a table to display the records model = new DefaultTableModel(); tblRecords = new JTable(model);

// Add columns to the table model.addColumn("Movie Name"); model.addColumn("Show Time"); model.addColumn("Ticket Price"); model.addColumn("Number of Seats"); model.addColumn("Total Price");

// Add the table to a scroll pane JScrollPane scrollPane = new JScrollPane(tblRecords); scrollPane.setBounds(20, 260, 760, 300); add(scrollPane); }

@Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btnAdd) { // Add a new record to the array list String[] record = new String[5]; record[0] = txtMovieName.getText(); record[1] = txtShowTime.getText(); record[2] = txtMoviePrice.getText(); record[3] = txtNumSeats.getText(); record[4] = txtPriceTotal.getText(); records.add(record);

// Add the record to the table model.addRow(record);

// Clear the text fields txtMovieName.setText(""); txtShowTime.setText(""); txtMoviePrice.setText(""); txtNumSeats.setText(""); txtPriceTotal.setText(""); } else if (e.getSource() == btnUpdate) { // Update a record in the array list int selectedRow = tblRecords.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, "Please select a record to update."); return; }

String[] record = new String[5]; record[0] = txtMovieName.getText(); record[1] = txtShowTime.getText(); record[2] = txtMoviePrice.getText(); record[3] = txtNumSeats.getText(); record[4] = txtPriceTotal.getText(); records.set(selectedRow, record);

// Update the record in the table model.setValueAt(record[0], selectedRow, 0); model.setValueAt(record[1], selectedRow, 1); model.setValueAt(record[2], selectedRow, 2); model.setValueAt(record[3], selectedRow, 3); model.setValueAt(record[4], selectedRow, 4);

// Clear the text fields txtMovieName.setText(""); txtShowTime.setText(""); txtMoviePrice.setText(""); txtNumSeats.setText(""); txtPriceTotal.setText(""); } else if (e.getSource() == btnDelete) { // Delete a record from the array list int selectedRow = tblRecords.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, "Please select a record to delete."); return; }

records.remove(selectedRow);

// Delete the record from the table model.removeRow(selectedRow);

// Clear the text fields txtMovieName.setText(""); txtShowTime.setText(""); txtMoviePrice.setText(""); txtNumSeats.setText(""); txtPriceTotal.setText("");

} else if (e.getSource() == btnPrint) { // Print the records to a text file try { FileWriter writer = new FileWriter("cinema_records.txt"); for (String[] record : records) { for (String field : record) { writer.write(field + ","); } writer.write(" "); }

writer.close(); JOptionPane.showMessageDialog(this, "Records printed to file."); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "Error printing records to file."); } } }

public static void main(String[] args) { // Create a new instance of the cinema booking management system CinemaManagementSystem bookingSystem = new CinemaManagementSystem();

// Set the size of the window bookingSystem.setSize(800, 600);

// Set the window to exit the application when closed bookingSystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Center the window on the screen bookingSystem.setLocationRelativeTo(null);

// Display the window bookingSystem.setVisible(true); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!