I am writing a Java program, a MP3 Manager, but I have problems to work with buttons, TextFields, and TextArea.


Here are the problem 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.


I started to write these programs. Here is the 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, but I cannot finish it. I really need help to finish this.

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 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();
		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);
		
		add(container, BorderLayout.NORTH );
		
		JTextArea textArea = new JTextArea();
		
		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 my MP3ManagerTest.java

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

The program should have been looked like the photo that I posted with. I am a new programmer, who really don't know much about Java. I am really appreciated if anyone could have helped with me. Thank in advance.

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

Can you explain what your problem with more specific questions?

Please edit your post and wrap the code in code tags. Use the [code] icon above the input box.

When I run it, it says that "Exception in thread 'main' java.lang.IllegalArgumentException: adding container's parent to itself at java.awt.Container.add<MP3Manager.java:65>"

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);
	}
}
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 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();
		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);
		
		add(container, BorderLayout.NORTH );
		
		JTextArea textArea = new JTextArea();
		
		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);
		}
		
	}
}
import javax.swing.*;
import java.awt.*;
//class
public class MP3ManagerTest{
	//mian
	public static void main(String [] args){
		
		MP3Manager manager = new MP3Manager();
		JFrame application = new JFrame();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		application.add(manager);
		application.setSize(650,850);
		application.setVisible(true);
	}
	
}

Therefore, I could not see what it was look like, but somehow I don't think it will be looked like the example in the attachment photo.

illegalArgumentException: adding container's parent to itself

That message says it all. Look at your code. That is exactly what it is doing.
What is the value of container?
What are you adding it to?

I have no idea what that sentence mean, I don't really know what is the value for the container, I think it might be JPanel?!

Look at your code. Where did the value of container come from?
Where does add() try to put the component you are adding?
Does the error message describe what you are trying to do?

I don't know where it come from, but I want to add it to the master panel, the largest panel. So I did this

panel = new JPanel();
		
panel.add(container, BorderLayout.NORTH );
		
JTextArea textArea = new JTextArea();
		
panel.add(textArea, BorderLayout.CENTER);

of course, I add a private JPanel panel in the beginning

Now, it says that "Exception in thread 'main' java.lang.NumberForException: For input string: '' at MP3Manager.java: 71, 79, 81".

Certainly, I have no idea what that means, and how to fix it.

Edited 5 Years Ago by Danielhuo: n/a

What method are you calling at the line where the error occurs?
The data that method received is an empty String, not a valid number.

When you post error message, post the whole text of the message. It has a lot of information that you have left off.

I am calling the buttonHandler method, and in it I am calling parseInt method

Where does the parseInt method get its String from?
Change that location to have a valid numeric String like "000000"

The parse method get its string from JTextField by using trackLengthField.getText(), but how should I set the string in trackLengthField to be "000000" where it has null as default.

Edited 5 Years Ago by Danielhuo: n/a

It still has the same problem, here is what did

trackLengthLabel = new JLabel("Track Length (in seconds) ");
trackLengthField = new JTextField([B]16[/B]);
fieldPanelTrackLength = new JPanel();

and it says the same thing

Go back and read the API doc for the JTextField class to see how to give it an initial value.

Okay, I read it, and here is what I did

trackLengthField = new JTextField("325", 16);

Then, I run it again, and here is what it said, "Exception in thread 'mian' java.lang.IllegealArgumentException: adding a window to a container

at java.awt.Container.checkNotAWindow(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at MP3ManagerTest.main(MP3ManagerTest.java: 11)"

I always to do this

import javax.swing.*;
import java.awt.*;
//class
public class MP3ManagerTest{
//mian
	public static void main(String [] args){

		MP3Manager manager = new MP3Manager();
		JFrame application = new JFrame();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		application.add(manager);
		application.setSize(650,850);
		application.setVisible(true);
	}
}

in main

So, it is wrong to write that "application.add(manager)"

IllegealArgumentException: adding a window to a container

The error message says what the problem is.
Is manager a window?

You need to look at the code and see what datatype the variable manager is. Then if you don't know for sure, look in the API doc to see if that class/datatype extends Window.

Somehow the other thread is closed, but what I meant is I should not use an inner class in the test program to run it (according to my instruction, which indicates that I will only have totally 7 files after I compile this project).

It executes with no problem, but in the test program I have to use that inner class in order to run it, which should not be that way. I am asking how I can write the code without that inner class in the test program.

You need to show the code you are talking about.
When you talk about classes you should give their names.

This question has already been answered. Start a new discussion instead.