Question

is there anyway you can modify the code so that when i run it i can...

is there anyway you can modify the code so that when i run it i can see all the song when i click the select button all the songs in the songList.txt file can be shown

song.java

package proj2;

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;
}

}

songdatabase.java:

package proj2;

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 = "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
}
}

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JCOmboBox is unable to handle duplicate names, so adding a unique value in the name works fine. Changes are being made at line 72.
So now when you select any value from dropdown, you get results relating to that song only.

package com.HomeworkLib;

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 = "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);
//Due to duplicate values in combo Box, Their was problem displaying values, adding a unique value to the name resolves the issue
cbSelectSong.addItem(i + 1 + ". " + 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
}
}

============= Edit ===========

In my system the program is running fine, I have uploaded the screenshots. What problem do you have?

1. Code is not executing.

2. You can't see song names in dropdown(JComboBox)

For 1st problem, I would recommend you to update java(version 8 update 162), then copy-paste my code and run.

Song Database XSong Database Select Song 1. Yellow Submarine Select Song 6. Blue 1 Item Code BT012 Item Code BTO19 Descriptio

Add a comment
Know the answer?
Add Answer to:
is there anyway you can modify the code so that when i run it i can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone modify my code so that I do not get this error: Exception in thread...

    Can someone modify my code so that I do not get this error: Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1043) at java.awt.Container.add(Container.java:363) at RatePanel.<init>(RatePanel.java:64) at CurrencyConverter.main(CurrencyConverter.java:16) This code should get the amount of money in US dollars from user and then let them select which currency they are trying to convert to and then in the textfield print the amount //********************************************************************* //File name: RatePanel.java //Name: Brittany Hines //Purpose: Panel for a program that convers different currencyNamerencies to use dollars //*********************************************************************...

  • Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...

    Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...

  • I tried to add exception handling to my program so that when the user puts in...

    I tried to add exception handling to my program so that when the user puts in an amount of change that is not the integer data type, the code will catch it and tell them to try again, but my try/catch isnt working. I'm not sure how to fix this problem. JAVA code pls package Chapter9; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUIlab extends JFrame implements ActionListener {    private static final int WIDTH = 300;    private...

  • Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with...

    Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with the files available here. public class app6 { public static void main(String args[]) {     myJFrame6 mjf = new myJFrame6(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJFrame6 extends JFrame {    myJPanel6 p6;    public myJFrame6 ()    {        super ("My First Frame"); //------------------------------------------------------ // Create components: Jpanel, JLabel and JTextField        p6 = new myJPanel6(); //------------------------------------------------------...

  • Can you help me to link two frames in Java Swing and then if you click...

    Can you help me to link two frames in Java Swing and then if you click "Proceed button" it will link to the Second Frame...Thank you :) First frame code: //First import javax.swing.*; import java.awt.event.*; import java.io.*; public class Sample extends JFrame implements ActionListener { JMenuBar mb; JMenu file; JMenuItem open; JTextArea ta; JButton proceed= new JButton("Proceed"); Sample() { open = new JMenuItem("Open File"); open.addActionListener(this); file = new JMenu("File"); file.add(open); mb = new JMenuBar(); mb.setBounds(0, 0, 400, 20); mb.add(file); ta...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • Please design a Java GUI application with two JTextField for user to enter the first name...

    Please design a Java GUI application with two JTextField for user to enter the first name and last name. Add two JButtons with action events so when user click on one button to generate a full name message from the user input and put it in a third JTextField which is not editable; and click on the other button to clear all three JTextField boxes. Please run the attached nameFrame.class file (Note: because there is an actionhandler inner class in...

  • Please fix and test my code so that all the buttons and function are working in...

    Please fix and test my code so that all the buttons and function are working in the Sudoku. CODE: SudokuLayout.java: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class SudokuLayout extends JFrame{ JTextArea txtArea;//for output JPanel gridPanel,butPanel;//panels for sudoku bord and buttons JButton hint,reset,solve,newPuzzel;//declare buttons JComboBox difficultyBox; SudokuLayout() { setName("Sudoku Bord");//set name of frame // setTitle("Sudoku Bord");//set...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • GUI in java: Run the below code before doing the actionlistener: import java.awt.*; import javax.swing.*; public...

    GUI in java: Run the below code before doing the actionlistener: import java.awt.*; import javax.swing.*; public class DrawingFrame extends JFrame {    JButton loadButton, saveButton, drawButton;    JComboBox colorList, shapesList;    JTextField parametersTextField;       DrawingFrame() {        super("Drawing Application");        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        JToolBar toolbar = new JToolBar();        toolbar.setRollover(true);        toolbar.add(loadButton=new JButton("Load"));        toolbar.add(saveButton=new JButton("Save"));        toolbar.addSeparator();        toolbar.add(drawButton=new JButton("Draw"));               toolbar.addSeparator();        toolbar.addSeparator();        toolbar.add(new...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT