Reputation: 35
I'm new to Java and I keep getting errors on compile, notably the "cannot find symbol error." I'm trying to get the string in the table to go in as the file destination, but I can't call the table so I tried using the variable holder, only to get an error again.
Along with that I was trying to change what kinds of files can be opened on the filechooser, but I can't find a solution that will work.
And finally, could someone maybe explain what a missing symbol is? Because I feel like I'm misunderstanding it since I've tried to fix it a bunch of times with no success.
//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;
String Artist, Song, Album, Loc;
Object[][] data;
int n = 1;
String holder;
//Makes the library, with a 51 song limit.
jLibrary[] addedSong = new jLibrary[50];
public 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]
}
};
//Creates table
final Jtable 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);
holder = table.getselectedRows[3];
}
//Creates action listener for button
public void actionPerformed(ActionEvent e) {
if (e.getSource() == save) {
dialog.setFileFilter(new FileNameExtensionFiler("Wave 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) {
try {
File soundFile = new File(holder);
System.out.println(soundFile);
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;
}
}
Error:
--------------------Configuration: <Default>--------------------
C:\Users\Andrew\Documents\ICS4U Final\jPlayer.java:47: error: cannot find symbol
final Jtable table = new JTable(data, columnNames);
^
symbol: class Jtable
location: class jPlayer
C:\Users\Andrew\Documents\ICS4U Final\jPlayer.java:75: error: cannot find symbol
dialog.setFileFilter(new FileNameExtensionFiler("Wave File (*.wav)"));
^
symbol: class FileNameExtensionFiler
location: class jPlayer
2 errors
Process completed.
Sorry for all the questions, thanks for any help!
Edit: I've changed Jtable to JTable, as well as added the t to filter, and gotten rid of the holder since I shouldn't need it anymore.
The code now looks like:
//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;
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() {
//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]
}
};
//Creates table
final JTable 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("Wave 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) {
try {
File soundFile = new File(table.getSelectedRows[3]);
System.out.println(soundFile);
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;
}
}
But now I'm getting this error:
--------------------Configuration: <Default>--------------------
C:\Users\Andrew\Documents\ICS4U Final\JPlayer.java:83: error: cannot find symbol
File soundFile = new File(table.getSelectedRows[3]);
^
symbol: variable table
location: class JPlayer
1 error
Process completed.
Any help?
Upvotes: 2
Views: 3451
Reputation: 17246
In answer to your second question;
Remember that variables defined inside methods can only be used within that method,
final JTable table = new JTable(data, columnNames);
is defined within the constructor public JPlayer()
so can't then be used in the public void actionPerformed(ActionEvent e)
method. You need to define table as a instance member(i.e. near the top* along with JButton save, play, stop, loop;
etc)
*technically instance members don't need to be near the top but they usually are
Upvotes: 0
Reputation: 31283
The class you need is JTable
, not Jtable
The error basically means that you are using a symbol that does not exist. The class you are trying to use exists in package javax.swing
(that you have imported) : javax.swing.JTable
, with a big T
An IDE would have helped you to find it but for a beginner, it is also a good thing to start with just a text editor and see errors one by one ;)
Upvotes: 6
Reputation: 301
final JTable table = new JTable(data, columnNames);
And not Jtable
. Try using eclipse for avoid this kind of problem !
Upvotes: 0
Reputation: 4555
The class is called FileNameExtensionFilter
- not FileNameExtensionFiler
. (add a 't').
I recommend you to download and use an IDE such as Eclipse or NetBeans. They will assist you in avoiding these typographic errors.
Upvotes: 1