954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to use Java JFrame with ButtonHandler, TextField, and TextArea

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.

Attachments 1.png 15.03KB
Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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"

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);
	}
	
}
Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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.

Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
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?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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?!

Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

What happens now when you execute the program?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.

Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Make sure the parseInt method gets a valid String.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

And how can I do that? Could you give me the fixed code for me?

Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.

Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Read the API doc for the JTextField class to see how to give it an initial value.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

It still has the same problem, here is what did

trackLengthLabel = new JLabel("Track Length (in seconds) ");
trackLengthField = new JTextField(<strong>16</strong>);
fieldPanelTrackLength = new JPanel();


and it says the same thing

Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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)"

Danielhuo
Light Poster
27 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: