I am writing a program, but I cannot find what is wrong with the code.

Only need to show one MP3, or an error message if there are any missing fields or an incorrect entry for seconds. It is not storing information about previous songs; it just display the current MP3.

Here are the problem entire descriptions.

Create a class MP3 with instance variables for: artist, song, album, track length (in seconds). Provide a constructor, get/set methods, and a toString. method. The toString method should convert the track length into minutes and seconds. For example, 265 seconds would be displayed as 4:25.

Create a class MP3Manager that extends JFrame with the following: a JPanel with a GridLayout that contains 4 labels, 4 textfields, and 4 buttons. This will be added to the container’s NORTH position.

The textfields are for user input of an MP3 record: artist, song, album, track length.

One button will be used to add an MP3 to a text file containing a list of MP3 files.

A second button will display all MP3s stored in the file.

The third button will find and display all MP3 files if the user enters the artist’s name and/or or the album name. The delete button will delete a single MP3 from the file based on the song title and artist name.

A JTextArea to display the MP3s. Add it to the container’s CENTER position.

Use a named inner class ButtonHandler to handle the button events. Note: the only event you will enable will be to add an MP3 (button 1). Just capture the input data in the 4 fields, create an instance of the MP3 class, and call the toString method to display it in the text area. If data is missing from one of the fields, inform the user can be informed by the TextArea.

Create an application MP3ManagerTest.

Here is my MP3 class:

public class MP3{

	private String artist;
	private String song;
	private String album;
	private int trackLength;
	
	public MP3(String artistName, String songName, String albumName, int trackLeng){
	
		setArtist(artistName);
		setSong(songName);
		setAlbum(albumName);
		setTrackLength(trackLeng);
		
	}
	
	public void setArtist(String artistName){
		artist = artistName;
	}
	
	public String getArtist(){
		return artist;
	}
	
	public void setSong(String songName){
		song = songName;
	}

	public String getSong(){
		return song;
	}
	
	public void setAlbum(String albumName){
		album = albumName;
	}
	
	public String getAlbum(){
		return album;
	}
	
	public void setTrackLength(int trackLeng){
		trackLength = trackLeng;
	}
	
	public int getTrackLength(){
		return trackLength;  
	}
	
	public String toString(){
		return String.format("%s, %s, %s, %d : %d", getArtist(), 
			getSong(), getAlbum(), getTrackLength() / 60,  
			getTrackLength() - (getTrackLength() / 60) * 60);
	}
}

And here is the MP3Manager class,

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MP3Manager extends JFrame{
	
	private JButton addMP3Button, displayMP3sButton, findMP3Button, deleteMP3Button;
	private JLabel artistLabel, songLabel, albumLabel, trackLengthLabel;
	private JTextField artistField, songField, albumField, trackLengthField;
	private JPanel panel, buttonPanel, fieldPanelArtist, fieldPanelSong, fieldPanelAlbum, fieldPanelTrackLength;
	
	public MP3Manager(){
	
		super("MP3 Manager");
		
		artistLabel = new JLabel("Artist Name ");
		artistField = new JTextField();
		fieldPanelArtist = new JPanel();
		fieldPanelArtist.setLayout(new FlowLayout());
		fieldPanelArtist.add(artistLabel);
		fieldPanelArtist.add(artistField);
		
		songLabel = new JLabel("Song Title ");
		songField = new JTextField();
		fieldPanelSong = new JPanel();
		fieldPanelSong.setLayout(new FlowLayout());
		fieldPanelSong.add(songLabel);
		fieldPanelSong.add(songField);
		
		albumLabel = new JLabel("Album Name ");
		albumField = new JTextField();
		fieldPanelAlbum = new JPanel();
		fieldPanelAlbum.setLayout(new FlowLayout());
		fieldPanelAlbum.add(albumLabel);
		fieldPanelAlbum.add(albumField);
		
		trackLengthLabel = new JLabel("Track Length (in seconds) ");
		trackLengthField = new JTextField("325", 16);
		fieldPanelTrackLength = new JPanel();
		fieldPanelTrackLength.setLayout(new FlowLayout());
		fieldPanelTrackLength.add(trackLengthLabel);
		fieldPanelTrackLength.add(trackLengthField);
		
		addMP3Button = new JButton(" Add MP3 ");
		displayMP3sButton = new JButton(" Display MP3s ");
		findMP3Button = new JButton(" Find MP3 ");
		deleteMP3Button = new JButton(" Delete MP3 ");
			
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(2, 2, 5, 5));
		buttonPanel.add(addMP3Button);
		buttonPanel.add(displayMP3sButton);
		buttonPanel.add(findMP3Button);
		buttonPanel.add(deleteMP3Button);
		
		Container container = getContentPane();
		container.setLayout(new GridLayout(5, 1, 5, 5));
		container.add(fieldPanelArtist);
		container.add(fieldPanelSong);
		container.add(fieldPanelAlbum);
		container.add(fieldPanelTrackLength);
		container.add(buttonPanel);
		
		panel = new JPanel();
		
		panel.add(container, BorderLayout.NORTH );
		
		JTextArea textArea = new JTextArea();
		
		panel.add(textArea, BorderLayout.CENTER);
		
		ButtonHandler handler = new ButtonHandler();
		addMP3Button.addActionListener(handler);
		displayMP3sButton.addActionListener(handler);
		findMP3Button.addActionListener(handler);
		deleteMP3Button.addActionListener(handler);
		
	}
	
	private class ButtonHandler implements ActionListener{
		
		int length = Integer.parseInt(trackLengthField.getText());
		String artist = artistField.getText();
		String album = albumField.getText();
		String song = songField.getText();
		MP3 list = new MP3(artist, song, album, length);
		String str = "";
		
		public void actionPerformed(ActionEvent event){
			
			if(event.getSource() == addMP3Button){
				if(artist == "" || album == "" || song == "" || trackLengthField.getText() == "")
					str = "Please fill in complete information";
				else if(length <= 0)
					str = "Please fill in integer for track length";
				else
					str = list.toString();
			}
			// else if(event.getSource() == displayMP3sButton)
			
			// else if(event.getSource() == findMP3Button)
			
			// else(event.getSource() == deleteMP3Button)

			TextArea textArea = new TextArea(str);
		}	
	}
}

And here is the MP3ManagerTest

import javax.swing.*;
import java.awt.*;
//class
public class MP3ManagerTest{
	//mian
	public static void main(String [] args){
		
		MP3Manager manager = new MP3Manager();
		manager.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		manager.setSize(650,650);
		manager.setVisible(true);
	}
}

After I run MP3ManagerTest, it has a window, but nothing on it.

Yes, I fixed some problem of the code. But Still I have a problem to write the test program.

Here is the code that works, but in the test program, I should not use that inner class to run it. And how am I suppose to do that?

public class MP3{
	// instance varables
	private String artist;
	private String song;
	private String album;
	private int trackLength;
	
	//constructor
	public MP3(String artistName, String songName, String albumName, int trackLeng){
	
		setArtist(artistName);
		setSong(songName);
		setAlbum(albumName);
		setTrackLength(trackLeng);
	}
	
	//set artist name
	public void setArtist(String artistName){
		artist = artistName;
	}
	
	//get artist name
	public String getArtist(){
		return artist;
	}
	
	//set song name
	public void setSong(String songName){
		song = songName;
	}

	//get song name
	public String getSong(){
		return song;
	}
	
	//set album name
	public void setAlbum(String albumName){
		album = albumName;
	}
	
	//get ablum name
	public String getAlbum(){
		return album;
	}
	
	// set track length
	public void setTrackLength(int trackLeng){
		trackLength = trackLeng;
	}
	
	// get track length
	public int getTrackLength(){
		return trackLength;  
	}
	
	// to string method
	public String toString(){
		return String.format("%s, %s, %s, %d : %d", getArtist(), 
			getSong(), getAlbum(), getTrackLength() / 60,  
			getTrackLength() - (getTrackLength() / 60) * 60);
	}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// class
public class MP3Manager extends JFrame{
	// four buttons, four fields, four panels, and text area
	private JButton addMP3Button, displayMP3sButton, findMP3Button, deleteMP3Button;
	private JLabel artistLabel, songLabel, albumLabel, trackLengthLabel;
	private JTextField artistField, songField, albumField, trackLengthField;
	private JPanel fieldPanelButtons, fieldPanelArtist, fieldPanelSong, fieldPanelAlbum, fieldPanelTrackLength;
	private JTextArea textArea;
	
	//constructor
	public MP3Manager(){
	
		super("MP3 Manager");
		
		// artist name label and field
		artistLabel = new JLabel("Artist Name ");
		artistField = new JTextField();
		artistField.setPreferredSize(new Dimension(180, 15));
		fieldPanelArtist = new JPanel();
		fieldPanelArtist.setLayout(new GridLayout(1, 2, 5, 5));
		fieldPanelArtist.add(artistLabel);
		fieldPanelArtist.add(artistField);
		
		//song name label and field
		songLabel = new JLabel("Song Title ");
		songField = new JTextField();
		songField.setPreferredSize(new Dimension(180, 15));
		fieldPanelSong = new JPanel();
		fieldPanelSong.setLayout(new GridLayout(1, 2, 5, 5));
		fieldPanelSong.add(songLabel);
		fieldPanelSong.add(songField);
		
		// album name label and field
		albumLabel = new JLabel("Album Name ");
		albumField = new JTextField();
		albumField.setPreferredSize(new Dimension(180, 15));
		fieldPanelAlbum = new JPanel();
		fieldPanelAlbum.setLayout(new GridLayout(1, 2, 5, 5));
		fieldPanelAlbum.add(albumLabel);
		fieldPanelAlbum.add(albumField);
		
		// track length name label and field
		trackLengthLabel = new JLabel("Track Length (in seconds) ");
		trackLengthField = new JTextField("", 16);
		albumField.setPreferredSize(new Dimension(180, 15));
		fieldPanelTrackLength = new JPanel();
		fieldPanelTrackLength.setLayout(new GridLayout(1, 2, 5, 5));
		fieldPanelTrackLength.add(trackLengthLabel);
		fieldPanelTrackLength.add(trackLengthField);
		
		// four buttons
		addMP3Button = new JButton(" Add MP3 ");
		displayMP3sButton = new JButton(" Display MP3s ");
		findMP3Button = new JButton(" Find MP3 ");
		deleteMP3Button = new JButton(" Delete MP3 ");
		
		//first two buttons
		JPanel fieldPanelButtonsA = new JPanel();
		fieldPanelButtonsA.setLayout(new GridLayout(1, 2, 5, 5));
		fieldPanelButtonsA.add(addMP3Button);
		fieldPanelButtonsA.add(displayMP3sButton);
		
		//second two buttons
		JPanel fieldPanelButtonsB = new JPanel();
		fieldPanelButtonsB.setLayout(new GridLayout(1, 2, 5, 5));
		fieldPanelButtonsB.add(findMP3Button);
		fieldPanelButtonsB.add(deleteMP3Button);
		
		// Panel
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(6, 1, 5, 5));
		panel.add(fieldPanelArtist);
		panel.add(fieldPanelSong);
		panel.add(fieldPanelAlbum);
		panel.add(fieldPanelTrackLength);
		panel.add(fieldPanelButtonsA);
		panel.add(fieldPanelButtonsB);
		
		//field panel
		JPanel fieldPanel = new JPanel();
		textArea = new JTextArea(50,30);
		textArea.setEditable(false);
		fieldPanel.add(panel, BorderLayout.NORTH);
		fieldPanel.add(textArea, BorderLayout.CENTER);
		
		this.setContentPane(fieldPanel);
		
		//handler
		ButtonHandler handler = new ButtonHandler();
		addMP3Button.addActionListener(handler);
		displayMP3sButton.addActionListener(handler);
		findMP3Button.addActionListener(handler);
		deleteMP3Button.addActionListener(handler);
	}
	
	//button handler class
	private class ButtonHandler implements ActionListener{
		// override action performed
		public void actionPerformed(ActionEvent event){
			int length = 0;
			String artist = artistField.getText();
			String album = albumField.getText();
			String song = songField.getText();
			
			//add mp3 button
			if(event.getActionCommand() == " Add MP3 "){
			
				//exception handling
				try{
					length = Integer.parseInt(trackLengthField.getText());
				}catch(Exception e){
					textArea.setText("Track Length must be integer.");
					return;
				}
				
				// if length is not positive
				if(length <= 0){
					textArea.setText("Track Length must be positive.");
					return;
				}
				
				//artist field is empty
				if(artist.trim().length() == 0){
					textArea.setText("Please fill out complete infotmation. \nArtist Field must be not empty.");
					return;
				}
				
				//album field is empty
				if(album.trim().length() == 0){
					textArea.setText("Please fill out complete infotmation. \nAblum Field must be not empty.");
					return;
				}
				
				//song field is empty
				if(song.trim().length() == 0){
					textArea.setText("Please fill out complete infotmation. \nSong Field must be not empty.");
					return;
				}
				 
				MP3 list = new MP3(artist, song, album, length);
				textArea.setText(list.toString());
			}
			// if(event.getActionCommand() == " Display MP3s ")
			// if(event.getActionCommand() == " Find MP3 ")
			// else(event.getActionCommand() == " Delete MP3 ")
		}
	}
}
import javax.swing.*;
import java.awt.*;
//class
public class MP3ManagerTest{
	//mian
	public static void main(String [] args){
		//inner class
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				//instance manager
				MP3Manager manager = new MP3Manager();
				manager.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				manager.setSize(new Dimension(400,400));
				manager.setVisible(true);
			}
		});
	}
}

in the test program, I should not use that inner class to run it. And how am I suppose to do that?

Please explain.

Thread close as it has double here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.