Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Runtime
I'm trying to build a project for an intro to java class where you have a library that you can add to and play music, kind of like a dumbed down iTunes. Unfortunately, after solving all of the syntax errors, I'm now getting one runtime when I open the savefiledialog. It won't show me files, only folders.
 
Here's my code:
 
//C:\\Users\\Andrew\\Downloads\\never gonna give you up.wav
//Andrew Douglas
//Imports
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.swing.filechooser.*;
import javax.swing.JTable;
 

//Creates class
public class JPlayer extends JFrame implements ActionListener {
 
	//Sets up form items and necessary globals
	JButton save, play, stop, loop;
	JFileChooser dialog;
	JTable table;
	String Artist, Song, Album, Loc;
	Object[][] data;
	int n = 1;
	//Makes the library, with a 51 song limit.
	JLibrary[] addedSong = new JLibrary[50];
 
	public JPlayer() {
		super ("JPlayer");
		//Creates frame
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      	this.setTitle("jPlayer");
      	this.setSize(800, 600);
      	//Makes titles for table
      	String[] columnNames =  {"Artist",
                                "Song",
                                "Album",
                                "Location"};
        //Gives one value for array
        addedSong[0] = new JLibrary ("Rick Astley", "NGGYU", "UnKnown", "C:\\Users\\Andrew\\Downloads\\never gonna give you up.wav");
        //Adds it to table array
        Object[][] data = {
        {
        	(addedSong[0].returnArtist()), (addedSong[0].returnSong()), (addedSong[0].returnAlbum()), (addedSong[0].returnFile())
        }
 
        };
		//Creates table
        table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        //Lets it sort the rows
        table.setAutoCreateRowSorter(true);
        //Creates the scroller
        JScrollPane scrollPane = new JScrollPane(table);
        //Makes the save file dialog and the play and save buttons
        dialog = new JFileChooser();
        play = new JButton ("Play Song");
        save = new JButton ("Save a file");
        //Adds the button listeners
        save.addActionListener(this);
        play.addActionListener(this);
        //Adds buttons to panel
        JPanel buttons = new JPanel();
        buttons.add(save);
        buttons.add(play);
        //Puts the buttons at the bottom
        add(buttons, BorderLayout.SOUTH);
        add(scrollPane);
      	this.setVisible(true);
 
	}
	//Creates action listener for button
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == save) {
			dialog.setFileFilter(new FileNameExtensionFilter("WAV File", ".wav"));
			int returnVal = dialog.showSaveDialog(JPlayer.this);
            if (returnVal == dialog.APPROVE_OPTION) {
                File file = dialog.getSelectedFile();
                addToLibrary("", "", "", file.getName());
 
            }
		}
		else if (e.getSource() == play) {
			String holder2;
			Object holder;
			holder = table.getValueAt(table.getSelectedRow(), 3);
			try {
			File soundFile = new File(holder.toString());
			AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
			Clip clip = AudioSystem.getClip();
			clip.open(audioIn);
         	clip.start();
			} catch (UnsupportedAudioFileException f) {
         f.printStackTrace();
      } catch (IOException f) {
         f.printStackTrace();
      } catch (LineUnavailableException f) {
         f.printStackTrace();
      }
 
	} }
	public static void main(String[]args) {
		new JPlayer();
	}
	public void addToLibrary(String art, String song, String alb, String file) {
			addedSong[n] = new JLibrary(art, song, alb, file);
			int j = 0;
			while (n >= 0) {
			Object[][] data = {
			{
				addedSong[(n-j)],
			}
		};
			j = j+1;
		}
			n = n +1;
 
	}
}
Any help would be appreciated, as I'm pretty new to thisSmile | :)
Posted 19-Jun-13 6:23am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

See this tutorial[^] for a sample of this control. Note also that if there are no .wav files currently stored in your directories then you will only see folders listed.
  Permalink  
Comments
Shubhashish_Mandal at 20-Jun-13 2:25am
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Richard MacCutchan at 20-Jun-13 3:25am
   
Isn't that the same link I already provided?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 431
1 Afzaal Ahmad Zeeshan 184
2 /\jmot 176
3 Sergey Alexandrovich Kryukov 175
4 Marcin Kozub 175
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 20 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100