The file songList.txt is at the very bottom of this message along with all the J
ID: 3834372 • Letter: T
Question
The file songList.txt is at the very bottom of this message along with all the JAVA SWING/FX Code
This my Java Swing code that I need help.
I hard wired the filename (String filename = "C:\Users\Deborah Ripley\songList.txt";) in the file but it won't use it. It immeditaly starts up with my Frame. It was working fine and i made some changes (not sure what i did). How can I get it to use the file? Need help ASAP. I have to hand it in tomorrow.
PROGRAM SONGDATABASE.JAVA
package eng605.songdatabase;
/**
* The application works as follows:
* Upon start-up, the application reads the database file. The path of the database file will be supplied by a run-
* time parameter. If the database file does not exist, the user will be told the database does not exist and prompted
* (non-GUI text prompt) asking if they want to create a new one. If the user answers positive, the application will
* continue with an initially empty song database. If the user answers negatively, the application will exit.
* An example of an appropriate start-up command is as follows: java SongDB mySongDB.data
* The original state of the frame shall display the combo box with the first song in the database selected.
* The Item Code, Description, Artist, Album, and Price fields for this item are displayed with the fields
* disabled (non-editable). The Add, Edit, Delete, and Exit buttons are enabled, and the Accept and Cancel buttons
* are disabled.
* To add a song, the user clicks on the Add button. This will cause the application to clear and enable the
* Item Code, Description, Artist, Album, and Price fields so the user can enter information for the new song.
* The Edit and Delete buttons are also disabled, and the Accept and Cancel buttons are enabled.
* When the user presses the Accept button, the new song will be added to the database and combo box.
* If the user presses the Cancel button, the entry transaction is canceled and the frame reverts to its original state.
* To edit an existing song, the user shall select the song from the combo box. The information for the selected
* song is displayed in the disabled fields. The user then presses the Edit button, which enables the Description,
* Artist, and Price fields. The user may not change the Item Code.
* The Add, Edit, and Delete buttons are disabled, and the Accept and Cancel buttons are enabled. If the user presses
* the Accept button, the changes are saved and the combo box is updated. If the user presses the Cancel button,
* the edit transaction is canceled and the frame reverts to its original state.
* To delete an existing song, the user selects the song from the combo box. The information for the song is
* displayed in the disabled fields. If the user presses the Delete button, the song is deleted from the database and
* the combo box.
* When the user presses the Exit button the application shall terminate. The current state of the database is
* saved in a file using the pathname supplied at program start-up (see above).
*
* Note: the prefix cb means combo box; lb means label; tf means textfield; con is for container.
*
* @param name is a string name assigned to the song;
* @param itemCode is a string code assigned to easch song in the database;
* @param description is a string describing the song in your database;
* @param artist is a string assigned to the song or album;;
* @param album a string assigned to the song pr album;;
* @param price is string assigned to the song or album;
**/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
public class SongDatabase extends JFrame implements ActionListener, ItemListener
{
private JButton addButton, editButton, deleteButton, acceptButton, cancelButton, exitButton;// saveButton;
private JLabel lbSelectSong, lbSongCode, lbSongDescription, lbSongArtist, lbSongAlbum, lbSongPrice;
private JTextField tfSongCode, tfSongDescription, tfSongArtist, tfSongAlbum, tfSongPrice;
private JComboBox cbSelectSong;
ArrayList<Song> songList;
boolean flag = false;
public static void main(String[] args)
{
new SongDatabase();
}
public void readFile()
{
String filename = "C:\Users\Deborah Ripley\songList.txt";
try
{
FileReader inputFile = new FileReader(filename);
Scanner parser = new Scanner(inputFile);
while(parser.hasNextLine())
{
String lineInfo = parser.nextLine();
String[] data = lineInfo.split(","); //Looking for a comma as the delimiter to parse the data
String name = data[0];
String itemCode = data[1];
String description = data[2];
String artist = data[3];
String album = data[4];
String price = data[5];
Song song = new Song(name, itemCode, description, artist, album, price);
songList.add(song);
}
inputFile.close();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "File does not exist", "Note", JOptionPane.INFORMATION_MESSAGE);
}
}
public SongDatabase()
{
songList = new ArrayList<Song>();
readFile();
arrangePosition();
Container con = this.getContentPane();
con.setLayout(null);
lbSelectSong = new JLabel("Select Song");
lbSelectSong.setBounds(100,30,120,25);
con.add(lbSelectSong);
cbSelectSong = new JComboBox();
for ( int i = 0; i < songList.size(); i++)
{
Song song = songList.get(i);
cbSelectSong.addItem(song.getName());
}
//*******add Select Song drop down Label and Field ***//
cbSelectSong.setBounds (200,30,200,25);
cbSelectSong.addItemListener (this);
add(cbSelectSong);
//*******add Item Code Label and Field ***********//
lbSongCode = new JLabel ("Item Code");
lbSongCode.setBounds (100,70,120,25);
con.add (lbSongCode);
tfSongCode = new JTextField(20);
tfSongCode.setBounds(200,70,200,25);
con.add(tfSongCode);
//*******add Description Label and Field ***********//
lbSongDescription = new JLabel("Description");
lbSongDescription.setBounds(100,110,120,25);
con.add(lbSongDescription);
tfSongDescription = new JTextField(20);
tfSongDescription.setBounds(200,110,200,25);
con.add(tfSongDescription);
//*******add Artist Label and Field ***********//
lbSongArtist = new JLabel("Artist");
lbSongArtist.setBounds(100,150,120,25);
con.add(lbSongArtist);
tfSongArtist = new JTextField(20);
tfSongArtist.setBounds(200,150,200,25);
con.add(tfSongArtist);
//*******add Album Label and Field ***********//
lbSongAlbum = new JLabel("Album");
lbSongAlbum.setBounds(100,190,120,25);
con.add(lbSongAlbum);
tfSongAlbum = new JTextField(20);
tfSongAlbum.setBounds(200,190,200,25);
con.add(tfSongAlbum);
//*******add Price Label and Field ***********//
lbSongPrice = new JLabel("Price");
lbSongPrice.setBounds(100,230,120,25);
con.add(lbSongPrice);
tfSongPrice = new JTextField(20);
tfSongPrice.setBounds(200,230,200,25);
con.add(tfSongPrice);
//**********************************************//
//*******add "ADD" Button ***********//
addButton=new JButton("Add");
addButton.setBounds(10,310,90,30);
addButton.addActionListener(this);
con.add(addButton);
//*******add "EDIT" Button ***********//
editButton=new JButton("Edit");
editButton.setBounds(110,310,80,30);
editButton.addActionListener(this);
con.add(editButton);
//*******add "DELETE" Button ***********//
deleteButton=new JButton("Delete");
deleteButton.setBounds(210,310,80,30);
deleteButton.addActionListener(this);
con.add(deleteButton);
//*******add "ACCEPT" Button ***********//
acceptButton=new JButton("Accept");
acceptButton.setBounds(310,310,80,30);
acceptButton.addActionListener(this);
con.add(acceptButton);
//*******add "CANCEL" Button ***********//
cancelButton=new JButton("Cancel");
cancelButton.setBounds(410,310,80,30);
cancelButton.addActionListener(this);
con.add(cancelButton);
//*******add " " Button ***********//
exitButton=new JButton("Exit");
exitButton.setBounds(170,370,80,30);
exitButton.addActionListener(this);
con.add(exitButton);
//*******add "SAVE" Button ***********//
// saveButton=new JButton("SAVE");
// saveButton.setBounds(270,370,80,30);
// saveButton.addActionListener(this);
// con.add(saveButton);
//********************************************
if(songList.size() > 0)
{
Song song = songList.get(0);
tfSongCode.setText(song.getItemCode());
tfSongDescription.setText(song.getDescription());
tfSongArtist.setText(song.getArtist());
tfSongAlbum.setText(song.getAlbum());
tfSongPrice.setText(song.getPrice());
}
tfSongCode.setEnabled(false); //the item Code field is disabled
tfSongDescription.setEnabled(false); //the Description field is disabled
tfSongArtist.setEnabled(false); //the Artist field is disabled
tfSongAlbum.setEnabled(false); //the Album field is disabled
tfSongPrice.setEnabled(false); //the Price field is disabled
addButton.setEnabled(true); // set the ADD button to be enabled
editButton.setEnabled(true); // set the EDIT button to be enabled
deleteButton.setEnabled(true); // set the DELETE button to be enabled
acceptButton.setEnabled(false); //the ACCEPT & CANCEL BUTTONS ARE DISABLED
cancelButton.setEnabled(false); //the ACCEPT & CANCEL BUTTONS ARE DISABLED
exitButton.setEnabled(true); // set the EXIT button to be enabled
// saveButton.setEnabled(true); // set the SAVE button to be enabled
setTitle("Song Database");
setSize(500,500);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void arrangePosition()
{
Toolkit tookit = Toolkit.getDefaultToolkit() ;
Dimension scDimentions = tookit.getScreenSize() ;
int scHeight = (int) scDimentions.getHeight() ;
int scWidth = (int) scDimentions.getWidth() ;
int x = ( scWidth - 500) / 2 ;
int y = ( scHeight - 600 ) / 2;
setLocation(x,y);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == addButton)
{
Toolkit.getDefaultToolkit().beep();
flag = false;
editButton.setEnabled(false); // set the EDIT button to be disabled
deleteButton.setEnabled(false); // set the DELETE button to disabled
acceptButton.setEnabled(true); // set the ACCEPT button to be enabled
cancelButton.setEnabled(true); // set the CANCEL button to be enabled
tfSongCode.setText("");
tfSongDescription.setText("");
tfSongArtist.setText("");
tfSongAlbum.setText("");
tfSongPrice.setText("");
tfSongCode.setEnabled(true); //the item CODE field is enabled
tfSongDescription.setEnabled(true); //the item DESCRIPTION field is enabled
tfSongArtist.setEnabled(true); //the item ARTIST field is enabled
tfSongAlbum.setEnabled(true); //the item ALBUM field is enabled
tfSongPrice.setEnabled(true); //the item PRICE field is enabled
}
if(e.getSource() == editButton)
{
Toolkit.getDefaultToolkit().beep();
flag = true;
tfSongCode.setEnabled(false); //the item CODE field is disabled
tfSongDescription.setEnabled(true); //the item DESCRIPTION field is enabled
tfSongArtist.setEnabled(true); //the item ARTIST field is enabled
tfSongAlbum.setEnabled(false); //the item ALBUM field is disabled
tfSongPrice.setEnabled(true); //the item PRICE field is enabled
addButton.setEnabled(false); // set the ADD button to be disabled
editButton.setEnabled(false); // set the EDIT button to be disabled
deleteButton.setEnabled(false); // set the DELETE button to disabled
acceptButton.setEnabled(true); // set the ACCEPT button to be enabled
cancelButton.setEnabled(true); // set the ACCEPT button to be enabled
exitButton.setEnabled(true); // set the CANCEL button to be enabled
// saveButton.setEnabled(true); // set the SAVE button to be enabled
tfSongDescription.setText("");
tfSongArtist.setText("");
tfSongPrice.setText("");
if(tfSongDescription.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song description", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongArtist.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song artist", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongPrice.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song price", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
}
if(e.getSource() == deleteButton)
{
}
if(e.getSource() == acceptButton)
{
if(tfSongCode.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song code", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongDescription.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song description", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongArtist.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song artist", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongAlbum.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song album", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongPrice.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song price", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
String name = tfSongDescription.getText();
String itemCode = tfSongCode.getText();
String description = tfSongDescription.getText();
String artist = tfSongArtist.getText();
String album = tfSongAlbum.getText();
String price = tfSongPrice.getText();
if(!flag)
{
Song song = new Song(name, itemCode, description, artist, album, price);
songList.add(song);
cbSelectSong.addItem(song.getName());
JOptionPane.showMessageDialog(null, "New song added into list", "Note", JOptionPane.INFORMATION_MESSAGE);
}
else
{
Song song = new Song(name, itemCode, description, artist, album, price);
int index = cbSelectSong.getSelectedIndex();
songList.set(index, song);
cbSelectSong.remove(index);
cbSelectSong.addItem(name);
JOptionPane.showMessageDialog(null, "new song updated successfully", "Note", JOptionPane.INFORMATION_MESSAGE);
}
}
if(e.getSource() == cancelButton)
{
if(songList.size() > 0)
{
cbSelectSong.setSelectedIndex(0);
Song song = songList.get(0);
tfSongCode.setText(song.getItemCode());
tfSongDescription.setText(song.getDescription());
tfSongArtist.setText(song.getArtist());
tfSongAlbum.setText(song.getAlbum());
tfSongPrice.setText(song.getPrice());
}
tfSongCode.setEnabled(false); //the item CODE field is disabled
tfSongDescription.setEnabled(false); //the item DESCRIPTION field is disabled
tfSongArtist.setEnabled(false); //the item ARTIST field is disabled
tfSongAlbum.setEnabled(false); //the item ALBUM field is disabled
tfSongPrice.setEnabled(false); //the item PRICE field is disabled
addButton.setEnabled(true); // set the ADD button to be enabled
editButton.setEnabled(true); // set the EDIT button to be enabled
deleteButton.setEnabled(true); // set the DELETE button to be enabled
acceptButton.setEnabled(false); // set the ACCEPT button to be disabled
cancelButton.setEnabled(false); // set the CANCEL button to be disabled
exitButton.setEnabled(true); // set the EXIT button to be enabled
// saveButton.setEnabled(true); // set the SAVE button to be enabled
}
if(e.getSource() == exitButton)
{
Toolkit.getDefaultToolkit().beep();
System.exit(0);
}
}
@Override
public void itemStateChanged(ItemEvent ie)
{
int index = cbSelectSong.getSelectedIndex();
Song song = songList.get(index);
tfSongCode.setText(song.getItemCode());
tfSongDescription.setText(song.getDescription());
tfSongArtist.setText(song.getArtist());
tfSongAlbum.setText(song.getAlbum());
tfSongPrice.setText(song.getPrice());
tfSongCode.setEnabled(false); //the item CODE field is disabled
tfSongDescription.setEnabled(false); //the item DESCRIPTION field is disabled
tfSongArtist.setEnabled(false); //the item ARTIST field is disabled
tfSongAlbum.setEnabled(false); //the item ALBUM field is disabled
tfSongPrice.setEnabled(false); //the item PRICE field is disabled
}
}
CLASS SONG.JAVA:
package eng605.songdatabase;
/**
* The application work as follows:
*
* Upon start-up, the application reads the database file. The path of the database file will be supplied by a run-
* time parameter. If the database file does not exist, the user will be told the database does not exist and prompted
* (non-GUI text prompt) asking if they want to create a new one. If the user answers positive, the application will
* continue with an initially empty song database. If the user answers negatively, the application will exit.
*
* The original state of the frame shall display the combo box with the first song in the database selected.
* The Item Code, Description, Artist, Album, and Price fields for this item are displayed with fields disabled.
* The Add, Edit, Delete, and Exit buttons are enabled, and the Accept and Cancel buttons are disabled.
*
* To add a song, User, 1. clicks the Add button, 2. application frame clears & enables Item Code, Description, Artist,
* Album, and Price fields 3. User can enter information for new song in Item Code, Description, Artist,
* Album, and Price fields. Edit and Delete buttons are disabled, but Accept and Cancel buttons
* are enabled. 4.User clicks Accept button, the new song will be added to the database and combo box.
* If the user presses the Cancel button, the entry transaction is canceled and the frame reverts to its original state.
*
* To edit an existing song, the User 1. selects the song from the combo box. The information for the selected
* song is displayed in the disabled fields. User 2. presses Edit button, which enables the Description,
* Artist, and Price fields. The user may not change the Item Code. The Add, Edit, and Delete buttons are disabled,
* and the Accept and Cancel buttons are enabled. If the User 3. presses the Accept button, the changes are saved and
* the combo box is updated. If the User 4. presses the Cancel button, the editted transaction is canceled & the frame
* reverts to its original state.
*
* To delete an existing song, the User 1. selects the song from the combo box. The information for the song is
* displayed in the disabled fields. If the User 2. presses the Delete button, the song is deleted from the database
* and the combo box.
*
* When the User 1. presses the Exit button the application terminates. The current state of the database is
* saved in a file using the pathname supplied at program start-up (see above).
*
* Note: the prefix cb means combo box; lb means label; tf means textfield; con is for container.
*
*
* @param name in a string name assigned to the song;
* @param itemCode a string code assigned to easch song in the database;
* @param description a string describing the song in your database;
* @param artist is assigned to the song or album;;
* @param album assigned to the song pr album;;
* @param price is assigned to the song or album;
**/
public class Song
{
private String name;
private String itemCode;
private String description;
private String artist;
private String album;
private String price;
Song(String name, String itemCode, String description, String artist, String album, String price)
{
this.name = name;
this.itemCode = itemCode;
this.description = description;
this.artist = artist;
this.album = album;
this.price = price;
}
/**
* Method getName gets the Name of the song
* @return returns the name of the song
**/
public String getName()
{
return name;
}
/**
*set Name assigns the name to this instance of name
**/
public void setName(String name)
{
this.name = name;
}
/**
* Method getItemCode gets the ItemCode of the song or album
* @return returns the ItemCode of the song or album
**/
public String getItemCode()
{
return itemCode;
}
/**
* Method setItemCode assigns the ItemCode to this instance of song or album
**/
public void setItemCode(String itemCode)
{
this.itemCode = itemCode;
}
/**
* Method getDescription gets the ItemCode of the song or album
* @return returns the description of the song or album
**/
public String getDescription()
{
return description;
}
/**
* Method setDescription assigns a description to this instance of description
**/
public void setDescription(String description)
{
this.description = description;
}
/**
* Method getArtist gets the name of Artist or Artists of the song or album
* @return returns the name of Artist or Artists of the instance of album
**/
public String getArtist()
{
return artist;
}
/**
* Method setArtist assigns the name of Artist or Artists to this instance of Artist
**/
public void setArtist(String artist)
{
this.artist = artist;
}
/**
* Method getAlbum gets the album name which the song came from
* @return returns a name of the album the song came from if there is an album
**/
public String getAlbum()
{
return album;
}
/**
* Method setAlbum assigns a name to this instance of album the song came from
**/
public void setAlbum(String album)
{
this.album = album;
}
/**
* Method getPrice gets the price of the song or album
* @return the price of the song or album
**/
public String getPrice()
{
return price;
}
/**
* Method setPrice sets the price to this instance of song or album
**/
public void setPrice(String price)
{
this.price = price;
}
}
songList.txt
Yellow Submarine, BT012, Yellow Submarine, Ringo Starr, The Beatles, 12.99
Yellow Submarine, BT015, Yellow Submarine, The Beatles, Beatles Greatest Hits 1, 10.99
New York New York, BT016, New York New York, Frank Sinatra, Trilogy Past Present Future, 9.99
Viva la Vida, BT017, Viva la Vida, Coldplay, Viva la Vida, 12.99
Unchained Melody, BT018, Unchained Melody, Billy Haliday, The Best of the Righteous Brothers, 13.99
Blue 1, BT019, Yellow Submarine, The Beatles, Beatles 1, 1.99
Blue 1, BT020, Yellow Submarine, The Beatles, Beatles 1, 1.99
Yellow Submarine, BT013, Yellow Submarine, The Beatles, Beatles Greatest Hits 1, 1.99
Yellow Submarine, BT014, Yellow Submarine, The Beatles, Beatles Greatest Hits 1, 1.99
Explanation / Answer
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
public class SongDatabase extends JFrame implements ActionListener, ItemListener
{
private JButton addButton, editButton, deleteButton, acceptButton, cancelButton, exitButton;// saveButton;
private JLabel lbSelectSong, lbSongCode, lbSongDescription, lbSongArtist, lbSongAlbum, lbSongPrice;
private JTextField tfSongCode, tfSongDescription, tfSongArtist, tfSongAlbum, tfSongPrice;
private JComboBox cbSelectSong;
ArrayList<Song> songList;
boolean flag = false;
public static void main(String[] args)
{
new SongDatabase();
}
public void readFile()
{
String filename = "C:\Users\Deborah Ripley\songList.txt";
try
{
FileReader inputFile = new FileReader(filename);
Scanner parser = new Scanner(inputFile);
while(parser.hasNextLine())
{
String lineInfo = parser.nextLine();
String[] data = lineInfo.split(","); //Looking for a comma as the delimiter to parse the data
String name = data[0];
String itemCode = data[1];
String description = data[2];
String artist = data[3];
String album = data[4];
String price = data[5];
Song song = new Song(name, itemCode, description, artist, album, price);
songList.add(song);
}
inputFile.close();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "File does not exist", "Note", JOptionPane.INFORMATION_MESSAGE);
}
}
public SongDatabase()
{
songList = new ArrayList<Song>();
readFile();
arrangePosition();
Container con = this.getContentPane();
con.setLayout(null);
lbSelectSong = new JLabel("Select Song");
lbSelectSong.setBounds(100,30,120,25);
con.add(lbSelectSong);
cbSelectSong = new JComboBox();
for ( int i = 0; i < songList.size(); i++)
{
Song song = songList.get(i);
cbSelectSong.addItem(song.getName());
}
//*******add Select Song drop down Label and Field ***//
cbSelectSong.setBounds (200,30,200,25);
cbSelectSong.addItemListener (this);
add(cbSelectSong);
//*******add Item Code Label and Field ***********//
lbSongCode = new JLabel ("Item Code");
lbSongCode.setBounds (100,70,120,25);
con.add (lbSongCode);
tfSongCode = new JTextField(20);
tfSongCode.setBounds(200,70,200,25);
con.add(tfSongCode);
//*******add Description Label and Field ***********//
lbSongDescription = new JLabel("Description");
lbSongDescription.setBounds(100,110,120,25);
con.add(lbSongDescription);
tfSongDescription = new JTextField(20);
tfSongDescription.setBounds(200,110,200,25);
con.add(tfSongDescription);
//*******add Artist Label and Field ***********//
lbSongArtist = new JLabel("Artist");
lbSongArtist.setBounds(100,150,120,25);
con.add(lbSongArtist);
tfSongArtist = new JTextField(20);
tfSongArtist.setBounds(200,150,200,25);
con.add(tfSongArtist);
//*******add Album Label and Field ***********//
lbSongAlbum = new JLabel("Album");
lbSongAlbum.setBounds(100,190,120,25);
con.add(lbSongAlbum);
tfSongAlbum = new JTextField(20);
tfSongAlbum.setBounds(200,190,200,25);
con.add(tfSongAlbum);
//*******add Price Label and Field ***********//
lbSongPrice = new JLabel("Price");
lbSongPrice.setBounds(100,230,120,25);
con.add(lbSongPrice);
tfSongPrice = new JTextField(20);
tfSongPrice.setBounds(200,230,200,25);
con.add(tfSongPrice);
//**********************************************//
//*******add "ADD" Button ***********//
addButton=new JButton("Add");
addButton.setBounds(10,310,90,30);
addButton.addActionListener(this);
con.add(addButton);
//*******add "EDIT" Button ***********//
editButton=new JButton("Edit");
editButton.setBounds(110,310,80,30);
editButton.addActionListener(this);
con.add(editButton);
//*******add "DELETE" Button ***********//
deleteButton=new JButton("Delete");
deleteButton.setBounds(210,310,80,30);
deleteButton.addActionListener(this);
con.add(deleteButton);
//*******add "ACCEPT" Button ***********//
acceptButton=new JButton("Accept");
acceptButton.setBounds(310,310,80,30);
acceptButton.addActionListener(this);
con.add(acceptButton);
//*******add "CANCEL" Button ***********//
cancelButton=new JButton("Cancel");
cancelButton.setBounds(410,310,80,30);
cancelButton.addActionListener(this);
con.add(cancelButton);
//*******add " " Button ***********//
exitButton=new JButton("Exit");
exitButton.setBounds(170,370,80,30);
exitButton.addActionListener(this);
con.add(exitButton);
//*******add "SAVE" Button ***********//
// saveButton=new JButton("SAVE");
// saveButton.setBounds(270,370,80,30);
// saveButton.addActionListener(this);
// con.add(saveButton);
//********************************************
if(songList.size() > 0)
{
Song song = songList.get(0);
tfSongCode.setText(song.getItemCode());
tfSongDescription.setText(song.getDescription());
tfSongArtist.setText(song.getArtist());
tfSongAlbum.setText(song.getAlbum());
tfSongPrice.setText(song.getPrice());
}
tfSongCode.setEnabled(false); //the item Code field is disabled
tfSongDescription.setEnabled(false); //the Description field is disabled
tfSongArtist.setEnabled(false); //the Artist field is disabled
tfSongAlbum.setEnabled(false); //the Album field is disabled
tfSongPrice.setEnabled(false); //the Price field is disabled
addButton.setEnabled(true); // set the ADD button to be enabled
editButton.setEnabled(true); // set the EDIT button to be enabled
deleteButton.setEnabled(true); // set the DELETE button to be enabled
acceptButton.setEnabled(false); //the ACCEPT & CANCEL BUTTONS ARE DISABLED
cancelButton.setEnabled(false); //the ACCEPT & CANCEL BUTTONS ARE DISABLED
exitButton.setEnabled(true); // set the EXIT button to be enabled
// saveButton.setEnabled(true); // set the SAVE button to be enabled
setTitle("Song Database");
setSize(500,500);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void arrangePosition()
{
Toolkit tookit = Toolkit.getDefaultToolkit() ;
Dimension scDimentions = tookit.getScreenSize() ;
int scHeight = (int) scDimentions.getHeight() ;
int scWidth = (int) scDimentions.getWidth() ;
int x = ( scWidth - 500) / 2 ;
int y = ( scHeight - 600 ) / 2;
setLocation(x,y);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == addButton)
{
Toolkit.getDefaultToolkit().beep();
flag = false;
editButton.setEnabled(false); // set the EDIT button to be disabled
deleteButton.setEnabled(false); // set the DELETE button to disabled
acceptButton.setEnabled(true); // set the ACCEPT button to be enabled
cancelButton.setEnabled(true); // set the CANCEL button to be enabled
tfSongCode.setText("");
tfSongDescription.setText("");
tfSongArtist.setText("");
tfSongAlbum.setText("");
tfSongPrice.setText("");
tfSongCode.setEnabled(true); //the item CODE field is enabled
tfSongDescription.setEnabled(true); //the item DESCRIPTION field is enabled
tfSongArtist.setEnabled(true); //the item ARTIST field is enabled
tfSongAlbum.setEnabled(true); //the item ALBUM field is enabled
tfSongPrice.setEnabled(true); //the item PRICE field is enabled
}
if(e.getSource() == editButton)
{
Toolkit.getDefaultToolkit().beep();
flag = true;
tfSongCode.setEnabled(false); //the item CODE field is disabled
tfSongDescription.setEnabled(true); //the item DESCRIPTION field is enabled
tfSongArtist.setEnabled(true); //the item ARTIST field is enabled
tfSongAlbum.setEnabled(false); //the item ALBUM field is disabled
tfSongPrice.setEnabled(true); //the item PRICE field is enabled
addButton.setEnabled(false); // set the ADD button to be disabled
editButton.setEnabled(false); // set the EDIT button to be disabled
deleteButton.setEnabled(false); // set the DELETE button to disabled
acceptButton.setEnabled(true); // set the ACCEPT button to be enabled
cancelButton.setEnabled(true); // set the ACCEPT button to be enabled
exitButton.setEnabled(true); // set the CANCEL button to be enabled
// saveButton.setEnabled(true); // set the SAVE button to be enabled
tfSongDescription.setText("");
tfSongArtist.setText("");
tfSongPrice.setText("");
if(tfSongDescription.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song description", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongArtist.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song artist", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongPrice.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song price", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
}
if(e.getSource() == deleteButton)
{
}
if(e.getSource() == acceptButton)
{
if(tfSongCode.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song code", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongDescription.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song description", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongArtist.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song artist", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongAlbum.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song album", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
if(tfSongPrice.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Enter song price", "Note", JOptionPane.INFORMATION_MESSAGE);
return;
}
String name = tfSongDescription.getText();
String itemCode = tfSongCode.getText();
String description = tfSongDescription.getText();
String artist = tfSongArtist.getText();
String album = tfSongAlbum.getText();
String price = tfSongPrice.getText();
if(!flag)
{
Song song = new Song(name, itemCode, description, artist, album, price);
songList.add(song);
cbSelectSong.addItem(song.getName());
JOptionPane.showMessageDialog(null, "New song added into list", "Note", JOptionPane.INFORMATION_MESSAGE);
}
else
{
Song song = new Song(name, itemCode, description, artist, album, price);
int index = cbSelectSong.getSelectedIndex();
songList.set(index, song);
cbSelectSong.remove(index);
cbSelectSong.addItem(name);
JOptionPane.showMessageDialog(null, "new song updated successfully", "Note", JOptionPane.INFORMATION_MESSAGE);
}
}
if(e.getSource() == cancelButton)
{
if(songList.size() > 0)
{
cbSelectSong.setSelectedIndex(0);
Song song = songList.get(0);
tfSongCode.setText(song.getItemCode());
tfSongDescription.setText(song.getDescription());
tfSongArtist.setText(song.getArtist());
tfSongAlbum.setText(song.getAlbum());
tfSongPrice.setText(song.getPrice());
}
tfSongCode.setEnabled(false); //the item CODE field is disabled
tfSongDescription.setEnabled(false); //the item DESCRIPTION field is disabled
tfSongArtist.setEnabled(false); //the item ARTIST field is disabled
tfSongAlbum.setEnabled(false); //the item ALBUM field is disabled
tfSongPrice.setEnabled(false); //the item PRICE field is disabled
addButton.setEnabled(true); // set the ADD button to be enabled
editButton.setEnabled(true); // set the EDIT button to be enabled
deleteButton.setEnabled(true); // set the DELETE button to be enabled
acceptButton.setEnabled(false); // set the ACCEPT button to be disabled
cancelButton.setEnabled(false); // set the CANCEL button to be disabled
exitButton.setEnabled(true); // set the EXIT button to be enabled
// saveButton.setEnabled(true); // set the SAVE button to be enabled
}
if(e.getSource() == exitButton)
{
Toolkit.getDefaultToolkit().beep();
System.exit(0);
}
}
@Override
public void itemStateChanged(ItemEvent ie)
{
int index = cbSelectSong.getSelectedIndex();
Song song = songList.get(index);
tfSongCode.setText(song.getItemCode());
tfSongDescription.setText(song.getDescription());
tfSongArtist.setText(song.getArtist());
tfSongAlbum.setText(song.getAlbum());
tfSongPrice.setText(song.getPrice());
tfSongCode.setEnabled(false); //the item CODE field is disabled
tfSongDescription.setEnabled(false); //the item DESCRIPTION field is disabled
tfSongArtist.setEnabled(false); //the item ARTIST field is disabled
tfSongAlbum.setEnabled(false); //the item ALBUM field is disabled
tfSongPrice.setEnabled(false); //the item PRICE field is disabled
}
}
CLASS SONG.JAVA:
package eng605.songdatabase;
/**
* The application work as follows:
*
* Upon start-up, the application reads the database file. The path of the database file will be supplied by a run-
* time parameter. If the database file does not exist, the user will be told the database does not exist and prompted
* (non-GUI text prompt) asking if they want to create a new one. If the user answers positive, the application will
* continue with an initially empty song database. If the user answers negatively, the application will exit.
*
* The original state of the frame shall display the combo box with the first song in the database selected.
* The Item Code, Description, Artist, Album, and Price fields for this item are displayed with fields disabled.
* The Add, Edit, Delete, and Exit buttons are enabled, and the Accept and Cancel buttons are disabled.
*
* To add a song, User, 1. clicks the Add button, 2. application frame clears & enables Item Code, Description, Artist,
* Album, and Price fields 3. User can enter information for new song in Item Code, Description, Artist,
* Album, and Price fields. Edit and Delete buttons are disabled, but Accept and Cancel buttons
* are enabled. 4.User clicks Accept button, the new song will be added to the database and combo box.
* If the user presses the Cancel button, the entry transaction is canceled and the frame reverts to its original state.
*
* To edit an existing song, the User 1. selects the song from the combo box. The information for the selected
* song is displayed in the disabled fields. User 2. presses Edit button, which enables the Description,
* Artist, and Price fields. The user may not change the Item Code. The Add, Edit, and Delete buttons are disabled,
* and the Accept and Cancel buttons are enabled. If the User 3. presses the Accept button, the changes are saved and
* the combo box is updated. If the User 4. presses the Cancel button, the editted transaction is canceled & the frame
* reverts to its original state.
*
* To delete an existing song, the User 1. selects the song from the combo box. The information for the song is
* displayed in the disabled fields. If the User 2. presses the Delete button, the song is deleted from the database
* and the combo box.
*
* When the User 1. presses the Exit button the application terminates. The current state of the database is
* saved in a file using the pathname supplied at program start-up (see above).
*
* Note: the prefix cb means combo box; lb means label; tf means textfield; con is for container.
*
*
* @param name in a string name assigned to the song;
* @param itemCode a string code assigned to easch song in the database;
* @param description a string describing the song in your database;
* @param artist is assigned to the song or album;;
* @param album assigned to the song pr album;;
* @param price is assigned to the song or album;
**/
public class Song
{
private String name;
private String itemCode;
private String description;
private String artist;
private String album;
private String price;
Song(String name, String itemCode, String description, String artist, String album, String price)
{
this.name = name;
this.itemCode = itemCode;
this.description = description;
this.artist = artist;
this.album = album;
this.price = price;
}
public String getName()
{
return name;
}
/**
*set Name assigns the name to this instance of name
**/
public void setName(String name)
{
this.name = name;
}
/**
* Method getItemCode gets the ItemCode of the song or album
* @return returns the ItemCode of the song or album
**/
public String getItemCode()
{
return itemCode;
}
/**
* Method setItemCode assigns the ItemCode to this instance of song or album
**/
public void setItemCode(String itemCode)
{
this.itemCode = itemCode;
}
/**
* Method getDescription gets the ItemCode of the song or album
* @return returns the description of the song or album
**/
public String getDescription()
{
return description;
}
/**
* Method setDescription assigns a description to this instance of description
**/
public void setDescription(String description)
{
this.description = description;
}
/**
* Method getArtist gets the name of Artist or Artists of the song or album
* @return returns the name of Artist or Artists of the instance of album
**/
public String getArtist()
{
return artist;
}
/**
* Method setArtist assigns the name of Artist or Artists to this instance of Artist
**/
public void setArtist(String artist)
{
this.artist = artist;
}
/**
* Method getAlbum gets the album name which the song came from
* @return returns a name of the album the song came from if there is an album
**/
public String getAlbum()
{
return album;
}
public void setAlbum(String album)
{
this.album = album;
}
public String getPrice()
{
return price;
}
public void setPrice(String price)
{
this.price = price;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.