Question
My question is How to implement the code below so that when the delete button is clicked the selected song should be deleted from the
My question is How to implement the code below so that when the delete button is clicked the selected song should be deleted from the song list? Currently, the click of delete button does not delete the selected song. Thank you!
Complete code to copy:
For the class Song.java:
// Definition of the class Song. public class Song { private String itemCode; private String description; private String artist; private String album; private double price; // Definition of the constructor. public Song() { this.itemCode = ""; this.description = ""; this.artist = ""; this.album = ""; this.price = 0.0; } // Definition of the parameterized Constructor. public Song(String itemCode, String description, String artist, String album, double price) { this.itemCode = itemCode; this.description = description; this.artist = artist; this.album = album; this.price = price; }
// Definition of the method getItemCode(). public String getItemCode() { return itemCode; } //Definition of the method setItemCode(). public void setItemCode(String itemCode) { this.itemCode = itemCode; } //Definition of the method getDescription(). public String getDescription() { return description; } //Definition of the method setDescription(). public void setDescription(String description) { this.description = description; } //Definition of the method getArtist(). public String getArtist() { return artist; } //Definition of the method setArtist(). public void setArtist(String artist) { this.artist = artist; } //Definition of the method getAlbum(). public String getAlbum() { return album; } //Definition of the method setAlbum(). public void setAlbum(String album) { this.album = album; } //Definition of the method getPrice(). public double getPrice() { return price; } //Definition of the method setPrice(). public void setPrice(double price) { this.price = price; } //Definition of the method toString(). public String toString() { return description; } }
For the class SongDB.java:
// File: SongDB.java // Import the packages. import java.io.*; import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*;
// Definition of the class SongDB. public class SongDB extends JFrame implements ActionListener { // Declare an ArrayList. private ArrayList
// Definition of the Constructor with parameter // ArrayList of songs. public SongDB(ArrayList
songs.add(oneSong); combobox.addItem(oneSong); } // Add a combo box in the panel1. panel1.add(jlblSelect); panel1.add(combobox); // Add ItemCode in the panel2. panel2.add(jlblItemCode); panel2.add(jtfItemCode); // Add Description in the panel3. panel3.add(jlblDescription); panel3.add(jtfDescription); // Add Artist in the panel4. panel4.add(jlblArtist); panel4.add(jtfArtist); // Add Album in the panel5. panel5.add(jlblAlbum); panel5.add(jtfAlbum); // Add Price in the panel6. panel6.add(jlblPrice); panel6.add(jtfPrice); // Add buttons Add, Delete, Accept,and Cancel // in the panel7. panel7.add(jbtnAdd); panel7.add(jbtnEdit); panel7.add(jbtnDelete); panel7.add(jbtnAccept); panel7.add(jbtnCancel); // Add Exit in the panel8. panel8.add(jbtnExit); // Set all the panels as Flow layout. setLayout(new FlowLayout()); add(panel1); add(panel2); add(panel3); add(panel4); add(panel5); add(panel6); add(panel7); add(panel8); // Select a song in the combobox. Song currentSong = (Song) combobox.getSelectedItem(); showSong(currentSong); // If the user press Accept or Cancel button, // then the operation is cancelled. jbtnAccept.setEnabled(false); jbtnCancel.setEnabled(false); // Call the method addActionListener(). combobox.addActionListener(this); jbtnAdd.addActionListener(this); jbtnEdit.addActionListener(this); jbtnDelete.addActionListener(this); jbtnAccept.addActionListener(this); jbtnCancel.addActionListener(this); jbtnExit.addActionListener(this); }// End of the class SongsDB. // Definition of the method actionPerformed().
public void actionPerformed(ActionEvent ae) { // create an object source and call // getSource() method. Object source = ae.getSource(); Song currentSong; // Define a boolean variable. boolean isNewSong = false; // If the user clicks the combobox, then // a song in the combobox get selected. if (source == combobox) { currentSong = (Song) combobox.getSelectedItem(); showSong(currentSong);
} // If the user clicks the Add button, then // Itemcode, Description, Artist, Album, // and Price fields are enabled and the // buttons Add, Edit, and Delete are disabled.. else if (source == jbtnAdd) { isNewSong = true;
jtfItemCode.setText(""); jtfDescription.setText(""); jtfArtist.setText(""); jtfAlbum.setText(""); jtfPrice.setText("0");
jtfItemCode.setEnabled(true); jtfDescription.setEnabled(true); jtfArtist.setEnabled(true); jtfAlbum.setEnabled(true); jtfPrice.setEnabled(true);
jbtnAdd.setEnabled(false); jbtnEdit.setEnabled(false); jbtnDelete.setEnabled(false);
jbtnAccept.setEnabled(true); jbtnCancel.setEnabled(true); } // If the user clicks the Edit button, then the // fields Description, Artist, Album, and Price // are enabled and the buttons Add, Delete, // Edit are disabled.the buttons Accept and // Cancel are enabled. else if (source == jbtnEdit) { isNewSong = false;
jtfDescription.setEnabled(true); jtfArtist.setEnabled(true); jtfAlbum.setEnabled(true); jtfPrice.setEnabled(true);
jbtnAdd.setEnabled(false); jbtnEdit.setEnabled(false); jbtnDelete.setEnabled(false);
jbtnAccept.setEnabled(true); jbtnCancel.setEnabled(true); } // if the user clicks the Delete button // the selected song is deleted. else if (source == jbtnDelete) { } // if the user clicks the Accept button then // a new song is added into the comobobox list. else if (source == jbtnAccept) { if (isNewSong) { currentSong = new Song(jtfItemCode.getText(), jtfDescription.getText(), jtfArtist.getText(), jtfAlbum.getText(), Double.parseDouble(jtfPrice.getText()));
songs.add(currentSong); combobox.addItem(currentSong);
combobox.setSelectedIndex(songs.size() - 1); currentSong = (Song) combobox.getSelectedItem(); showSong(currentSong); } else { }
jbtnAccept.setEnabled(false); jbtnCancel.setEnabled(false);
jbtnAdd.setEnabled(true); jbtnEdit.setEnabled(true); jbtnDelete.setEnabled(true); } else if (source == jbtnCancel) { jbtnAccept.setEnabled(false); jbtnCancel.setEnabled(false);
jbtnAdd.setEnabled(true); jbtnEdit.setEnabled(true); jbtnDelete.setEnabled(true); } else // source == jbtnExit { System.exit(0); } }// End of the method actionPerformed().
// Definition of the method showSong(). private void showSong(Song currentSong) { // Display the contents in the fields ItemCode, // Description, Artist, Album, and Price. jtfItemCode.setText(currentSong.getItemCode()); jtfDescription.setText(currentSong.getDescription()); jtfArtist.setText(currentSong.getArtist()); jtfAlbum.setText(currentSong.getAlbum()); jtfPrice.setText("" + currentSong.getPrice()); // Disable the fields. jtfItemCode.setEnabled(false); jtfDescription.setEnabled(false); jtfArtist.setEnabled(false); jtfAlbum.setEnabled(false); jtfPrice.setEnabled(false); }// End of the method showSong().
// Definition of the main() method. public static void main(String[] args) { // Create an object of the ArrayList. ArrayList
Scanner input = new Scanner(System.in); // Prompt the user to enter the database // file name. System.out.print("Enter the database file name:"); // Read the file name. String filename = input.nextLine(); String choice = "";
try {
Scanner infile = new Scanner(new File(filename)); // Read the contents of the File. while (infile.hasNextLine()) { String line = infile.nextLine(); String[] tokens = line.split(","); // Add the songs in the file to the // songs list. Song newSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3], Double.parseDouble(tokens[4])); list.add(newSong); }
} // If the file is not found, then prompt // the user to create a new database. catch (FileNotFoundException e) { System.out.println("The database " + "file does not exist."); System.out.print("Do you want to" + " create a new one? (Y/N): "); choice = input.nextLine(); }
if (choice.charAt(0) == 'Y' || choice.charAt(0) == 'y') { // if the user wants to create a // database, then create a frame. SongDB sdb = new SongDB(list); // Set the title. sdb.setTitle("Team Roster"); // Set the dimensions of the frame. sdb.setSize(400, 350); sdb.setVisible(true); sdb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } else { System.out.println("Thank you"); } } }// End of the main() method.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started