Reputation: 3
I have an application for converting video to audio, it converts file correctly but it has two errors, first error is that: when once it completes converting file and i browse new file so it gives an error in thread like this :
Exception in thread "AWT-EventQueue-0"java.lang.IllegalThreadStateException at java.lang.Thread.start(Unknown Source)
an other error is that: progress bar is not working correctly this is code:
import it.sauronsoftware.jave.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class MultiThreadign {
static String x=null;
static File Audio;
public static void main(String[] aa)
{
final JFrame mainframe=new JFrame("Video To Mp3 Converter");
mainframe.setResizable(true);
final JPanel panel=new JPanel();
panel.setLayout(null);
panel.setBackground(Color.white);
mainframe.add(panel);
/* ********************** MAIN BODY ************************* */
JLabel browsefiles=new JLabel("Files");
browsefiles.setBounds(300, 50, 100, 25);
panel.add(browsefiles);
JLabel progressl=new JLabel("Status");
progressl.setBounds(600, 50, 100, 25);
panel.add(progressl);
// first task buttons start
final JTextField field1=new JTextField (300);
field1.setBounds(160, 80, 300, 25);
panel.add(field1);
final JProgressBar progress = new JProgressBar();
progress.setBounds(475, 80, 280, 25);
progress.setStringPainted(true);
progress.setMinimum(0);
progress.setMaximum(99);
panel.add(progress);
JButton browse=new JButton("Browse File");
browse.setBounds(10, 80, 130, 25);
panel.add(browse);
browse.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{ final JFileChooser chooser=new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.mp4, *.avi,*.3gp, *2gp", new String[] {"mp4","avi","3gp"});
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(mainframe) == JFileChooser.APPROVE_OPTION)
{
field1.setText(chooser.getSelectedFile().getName());
x=chooser.getSelectedFile().getAbsolutePath();
}
}
}); // browse files action ends here
final Thread t1=new Thread (new Runnable()
{
public void run()
{ mainframe.getContentPane().repaint();
mainframe.getContentPane().validate();
try
{
File Video=new File(x);
String z="Audio.mp3";
Audio=new File (z);
AudioAttributes audio=new AudioAttributes();
audio.setCodec("libmp3lame"); //mp3 format "libmp3lame"
int abc=128000;
audio.setBitRate(abc);
audio.setChannels (new Integer(2));
audio.setSamplingRate(new Integer(44100));
EncodingAttributes attr=new EncodingAttributes();
attr.setFormat("mp3");
attr.setAudioAttributes(audio);
Encoder encode=new Encoder();
long totalLength = Audio.length();
try
{
encode.encode(Video, Audio, attr);
FileReader fr=new FileReader(Audio);
@SuppressWarnings("resource")
BufferedReader br=new BufferedReader(fr);
long readLength = 0;
String s="";
while ((s = br.readLine()) != null) {
readLength += s.length();
progress.setValue((int) totalLength);
}
}
catch (Exception e)
{
System.out.print(e);
}
JOptionPane.showMessageDialog(null, "first Completed");
Audio=null;
}
catch (Exception e)
{
System.out.print(e);
}
//mainframe.repaint();
}
});
// first task button ends
final JButton start=new JButton("Start Converting");
start.setBounds(180, 260, 250, 25);
panel.add(start);
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if (x !=null)
{
t1.start();
JOptionPane.showMessageDialog(null, "process one started.");
x=null;
}
}});
/* ********************* MAIN BODY ENDS HERE ************* */
/* **************************** MENU BAR ACTIONS HERE ******************* */
mainframe.setSize(800, 400);
mainframe.setLocationRelativeTo(null);
mainframe.setVisible(true);
}
}
Upvotes: 0
Views: 443
Reputation: 1213
Firstly, your code is trying to use Swing components from threads other than the EDT. This will cause undefined behavior. That's why you are getting that Exception.
You should always create, modify, and access all of your Swing components on the AWT Event Dispatch Thread (EDT). Read about Concurrency in Swing.
Here is a short example of calling code on the EDT:
//Submits this Runnable to the queue to be run by on the EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("My Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add stuff to frame
frame.pack();
frame.setVisible(true);
}
});
Secondly, it would be better to use a SwingWorker
for a JProgressBar
. Read How to Use Progress Bars. You could use a Thread
if you really wanted to, but you would still have manage a way to update Swing components only on the EDT. That's what SwingWorker
is made for: to make it easy to perform long running operations in manner that doesn't block the EDT, with the option of periodically updating Swing components, such as a JProgressBar
, properly on the EDT.
Thirdly, you should avoid setting absolute sizes and positions for components and use Layout Managers instead.
Upvotes: 1