*******************************************************
COMPLETE SOURCE CODE FOR : MainClass.java
*******************************************************
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.filechooser.FileFilter;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.JLabel;
import javazoom.jl.converter.Converter;
public class MainClass extends JFrame implements ActionListener
{
JButton mainButton=new JButton ("Click Here To Select MP3 Files To Convert");
JTextArea myLabel=new JTextArea("\n\tAll converted file has name like original \n\tfile with added .wav at the end and located in the same \n\tdirectory with original mp3 file."
+ "\n\n\tYou can convert multiple files in a time.");
JLabel informPanel=new JLabel("STATUS : No File To Convert",SwingConstants.CENTER);
JPanel testingPanel=new JPanel();
JFileChooser jfc;
SwingWorker worker;
File selectedFile;
public MainClass()
{
super("Free Java MP3 To Wav Converter");
setLayout(new BorderLayout());
mainButton.addActionListener(this);
myLabel.setEditable(false);
myLabel.setSize(300, 400);
testingPanel.add(informPanel);
add(testingPanel,BorderLayout.SOUTH);
add(myLabel,BorderLayout.CENTER);
add(mainButton,BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(500,200);
setLocationRelativeTo(null);
setVisible(true);
}
void updateJlabel(final String jlabelText)
{
Runnable doSetProgressBarValue = new Runnable() {
public void run() {
informPanel.setText(jlabelText);
}
};
SwingUtilities.invokeLater(doSetProgressBarValue);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mainButton)
{
mainButton.setEnabled(false);
jfc=new JFileChooser();
jfc.setMultiSelectionEnabled(true);
jfc.setFileFilter(new TypeOfFile());
jfc.setDialogTitle("Select File To Convert");
int a=jfc.showDialog(this, "Convert");
if(a==JFileChooser.APPROVE_OPTION)
{
worker=new SwingWorker()
{
public Object construct()
{
return doWork();
}
public void finished()
{
mainButton.setEnabled(true);
updateJlabel("STATUS : No File To Convert");
//JOptionPane.showMessageDialog(null, "", "ERROR", JOptionPane.ERROR_MESSAGE);
}
};
worker.start();
}
else
{
mainButton.setEnabled(true);
}
}
}
Object doWork()
{
File [] groupOfFiles=jfc.getSelectedFiles();
int numberOfFiles=groupOfFiles.length;
for(int i=0;i<numberOfFiles;i++)
{
mainButton.enable(false);
selectedFile=groupOfFiles[i];
if(selectedFile.getName().toLowerCase().endsWith(".mp3"))
{
try
{
updateJlabel("Please Wait : "+selectedFile.getName()+" still in converting...");
Converter myConverter = new Converter();
myConverter.convert(selectedFile.getPath(), selectedFile.getPath()+".wav");
}
catch(Exception error)
{
//updateJlabel("Converting Process Fail For : "+selectedFile.getName());
JOptionPane.showMessageDialog(null, "Converting Process Fail", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
else
{
//updateJlabel(selectedFile.getName()+" not a MP3 File");
JOptionPane.showMessageDialog(null, selectedFile.getName()+" not a MP3 File", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
return new Object();
}
//Class TypeOfFile
class TypeOfFile extends FileFilter
{
//Type of file that should be display in JFileChooser will be set here
//We choose to display only directory and mp3 file
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".mp3")||f.isDirectory();
}
//Set description for the type of file that should be display
public String getDescription()
{
return ".mp3 files";
}
}
public static void main(String[]args)
{
MainClass myObject=new MainClass();
}
}
//I will put this complete source codes in my blog
*********************************************************************
COMPLETE SOURCE CODE FOR : SwingWorker.java
*********************************************************************
import javax.swing.SwingUtilities;
/**
* This is the 3rd version of SwingWorker (also known as
* SwingWorker 3), an abstract class that you subclass to
* perform GUI-related work in a dedicated thread. For
* instructions on using this class, see:
*
* http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
*
* Note that the API changed slightly in the 3rd version:
* You must now invoke start() on the SwingWorker after
* creating it.
*/
public abstract class SwingWorker {
private Object value; // see getValue(), setValue()
private Thread thread;
/**
* Class to maintain reference to current worker thread
* under separate synchronization control.
*/
private static class ThreadVar {
private Thread thread;
ThreadVar(Thread t) { thread = t; }
synchronized Thread get() { return thread; }
synchronized void clear() { thread = null; }
}
private ThreadVar threadVar;
/**
* Get the value produced by the worker thread, or null if it
* hasn't been constructed yet.
*/
protected synchronized Object getValue() {
return value;
}
/**
* Set the value produced by worker thread
*/
private synchronized void setValue(Object x) {
value = x;
}
/**
* Compute the value to be returned by the <code>get</code> method.
*/
public abstract Object construct();
/**
* Called on the event dispatching thread (not on the worker thread)
* after the <code>construct</code> method has returned.
*/
public void finished() {
}
/**
* A new method that interrupts the worker thread. Call this method
* to force the worker to stop what it's doing.
*/
public void interrupt() {
Thread t = threadVar.get();
if (t != null) {
t.interrupt();
}
threadVar.clear();
}
/**
* Return the value created by the <code>construct</code> method.
* Returns null if either the constructing thread or the current
* thread was interrupted before a value was produced.
*
* @return the value created by the <code>construct</code> method
*/
public Object get() {
while (true) {
Thread t = threadVar.get();
if (t == null) {
return getValue();
}
try {
t.join();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt(); // propagate
return null;
}
}
}
/**
* Start a thread that will call the <code>construct</code> method
* and then exit.
*/
public SwingWorker() {
final Runnable doFinished = new Runnable() {
public void run() { finished(); }
};
Runnable doConstruct = new Runnable() {
public void run() {
try {
setValue(construct());
}
finally {
threadVar.clear();
}
SwingUtilities.invokeLater(doFinished);
}
};
Thread t = new Thread(doConstruct);
threadVar = new ThreadVar(t);
}
/**
* Start the worker thread.
*/
public void start() {
Thread t = threadVar.get();
if (t != null) {
t.start();
}
}
}
//I will put this complete source codes in my blog
//My blog url is http://java2everyone.blogspot.com