The problem with my program is that once the "getMP3Tags" method is removed from the main class which is called ""musicManagerUI", and placed within it's own class, the method does not send anything over to the appropriate methods in the "musicManagerUI" class. However, if the "getMP3Tags" method is placed within the "musicManagerUI" class then everything works perfectly. I want to know why this happens and if anyone can help me to resolve this issue, as I don't want to cluster up the "musicManagerUI" class with loads of methods.
Here is the constructor for the main class "musicManagerUI":
public musicManagerUI() {
super ("Music Manager");
initComponents();
handlerClass handler = new handlerClass();
jMenuItem5.addMouseListener(handler);
}
Here is one the four methods which prints out data to the jTable:
public void printArtistName(ArrayList arrayOfArtistNames)
{
DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
if (arrayOfArtistNames == null) {model.setValueAt("No Artist Names Found", 0, 0);}
else {
Object [] rowData;
rowData = new Object[3];
for (Object x : arrayOfArtistNames)
{
model.addRow(rowData);
model.setValueAt(x, counterArtist, 0);
counterArtist++;
}
}
}
Here is the code for the mousepressed handler method:
public void mousePressed(MouseEvent e) {
DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
if (counterArtist == 0){}
else if (counterArtist > 0) {
model.setRowCount(0);
counterArtist = 0; counterTitle = 0; counterAlbum = 0; genre = 0;}
String FILE_DIR = "C:\\Users\\Hazzy\\Desktop\\test-data\\";
String FILE_TEXT_EXT = ".mp3";
findCertainExtension fw = new findCertainExtension();
try {
fw.getMP3Tags(FILE_DIR);
} catch (IOException ex) {
System.err.println(" Could Not read file in ");
} catch (TagException ex) {
System.err.println(" Could Not find tag");
}
}
And finally the "getMP3Tags" method:
public void getMP3Tags( String path ) throws IOException, TagException,
try{
File root = new File( path );
File[] list = root.listFiles();
ArrayList addArtistTitle = new ArrayList();
ArrayList addTrackTitle = new ArrayList();
ArrayList addAlbumTitle = new ArrayList();
ArrayList addGenre = new ArrayList();
if (list == null) return;
for ( File f : list ) {
if ( f.isDirectory()) {
getMP3Tags( f.getAbsolutePath() );
}
else if (!f.getAbsoluteFile().toString().toLowerCase().endsWith("mp3"))
{
getMP3Tags(f.getAbsolutePath());
}
else{
musicManagerUI setFields = new musicManagerUI ();
//System.out.println(f.getAbsoluteFile());
for (Object x : list) {}
MP3File mp3file = new MP3File(f.getAbsoluteFile());
ID3v2_2 name = (ID3v2_2) mp3file.getID3v2Tag();
if (name.getLeadArtist().isEmpty() == true){ name.setLeadArtist("Unknown");}
else if (name.getSongTitle().isEmpty() == true) {name.setSongTitle("Unknown");}
else if (name.getSongGenre().isEmpty() == true) {name.setSongGenre("Unknown");}
else if (name.getAlbumTitle().isEmpty() == true) {name.setAlbumTitle("Unknown");}
addArtistTitle.add(name.getLeadArtist());
addTrackTitle.add(name.getSongTitle());
addAlbumTitle.add(name.getAlbumTitle());
addGenre.add(name.getSongGenre());
}
}
musicManagerUI sendTrackInfo = new musicManagerUI();
sendTrackInfo.printArtistName(addArtistTitle);
sendTrackInfo.printTrackTitle(addTrackTitle);
sendTrackInfo.printAlbumTitle(addAlbumTitle);
sendTrackInfo.printGenre(addGenre);
}
catch(NullPointerException e){
System.err.println("Unknown Artist");
//return;
}
}
}
For this program I am using the ID3 Tagging Library/API. If you need the full code then please email me. Thanks in advance.
Some other sources which may be useful:
JTable does not show anything? Loading Java JTable: Why does it not work?