How to run ActionListenters in two different thread for the following code...
The ActionListeners here are not running concurrently...
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javazoom.jl.player.Player;
import java.io.FileInputStream;
public class Play2 extends JFrame
{
JButton b,b1;
JTextField t;
Play2()
{
JFrame j=new JFrame("MusicPlayer");
j.setSize(300,300);
j.setDefaultCloseOperation(EXIT_ON_CLOSE);
b=new JButton("Play");
b1=new JButton("Stop");
JPanel p=new JPanel();
t=new JTextField(20);
p.add(t);
p.add(b);
p.add(b1);
j.add(p);
/**try {
j.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("jesus.jpg")))));
}
catch (IOException e) {
e.printStackTrace();
}
j.getContentPane().setOpaque(false);*/
j.setVisible(true);
}
public void awt()
{
b.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
try
{
String fname=t.getText();
File directory = new File(fname);
boolean isDirectory = directory.isDirectory();
if (isDirectory)
{
// It returns true if directory is a directory.
System.out.println("the name you have entered is a directory : " + directory);
//It returns the absolutepath of a directory.
System.out.println("the path is " + directory.getAbsolutePath());
}
else
{
// It returns false if directory is a file.
System.out.println("the name you have entered is a file : " + directory);
//It returns the absolute path of a file.
System.out.println("the path is " + directory.getAbsolutePath());
}
String s=directory.getAbsolutePath();
s=s.replace("\\","/") ;
FileInputStream fis=new FileInputStream(s);
final Player playMp3=new Player(fis);
playMp3.play();
}
catch(Exception e){
System.out.println(e);}
}//end actionPerformed
}//end ActionListener
);//end addActionListener()
b1.addActionListener(
new ActionListener(){
public void actionPerformed(
ActionEvent ae){
//Terminate playback before EOF
try
{
String fname=t.getText();
File directory = new File(fname);
boolean isDirectory = directory.isDirectory();
if (isDirectory)
{
// It returns true if directory is a directory.
System.out.println("the name you have entered is a directory : " + directory);
//It returns the absolutepath of a directory.
System.out.println("the path is " + directory.getAbsolutePath());
}
else
{
// It returns false if directory is a file.
System.out.println("the name you have entered is a file : " + directory);
//It returns the absolute path of a file.
System.out.println("the path is " + directory.getAbsolutePath());
}
String s=directory.getAbsolutePath();
s=s.replace("\\","/") ;
FileInputStream fis=new FileInputStream(s);
final Player playMp3=new Player(fis);
playMp3.close();
}
catch(Exception e){
System.out.println(e);}
}//end actionPerformed
}//end ActionListener
);//end addActionListener()
}
public static void main(String[]args)
{
Play2 f=new Play2();
f.awt();
}
}
View Answers
Ads
Related Tutorials/Questions & Answers:
Advertisements
How to Explain different way of using thread?
How to Explain
different way of using
thread? Hi,
How to explain
how to using
different thread in Java program........
Hi,
There are
different types
Thread in Java program. Here is the explain
how to using
thread
Merging Two Arrays Of Different Lengths
Merging
Two Arrays Of
Different Lengths I have
two arrays of
different lengths and wants to have merged values into third. The only condition is, I want unique values in it(third array).
Thanks In Advance
Java code for following
Java
code for following Create a function that returns day difference between
two dates (inclusive), without using any function provided by the platform or external library. The function must work for all dates in the range of 1
Java Thread : run() method
Java
Thread :
run() method
In this section we are going to describe
run() method with example in java
thread.
Thread run() :
All the execution
code... will write all the
code here, which we want to
execute.
public void
run
Expain the following code ?
Expain the
following code ? please explain the
following line of
code clearly?
<html:link href="user.do?parameter=create">Create User</html:link>
Run a simple EJB code
Run a simple EJB code I found the
code this. However, as I have no idea with EJB, I can't understand
how to
run it. Can anybody help me by giving steps (by giving snapshots or writing full procedure)
how to
run it (any platform
Run a simple EJB code
Run a simple EJB code I found the
code this. However, as I have no idea with EJB, I can't understand
how to
run it. Can anybody help me by giving steps (by giving snapshots or writing full procedure)
how to
run it (any platform
core java code for following program
core java
code for
following program We are hosting the Olympic... should generate the
following reports
a. A medal tally
b. For a given country....
The
following are the guidelines while writing this program
Only command line based
different output trying to execute same java code
there is
different output trying to execute same java
code...
different output trying to execute same java code i am using net beans 7 ide and java 6 to develop my java projects. i used the
following coding
different output trying to execute same java code
different output trying to execute same java code i am using net beans 7 ide and java 6 to develop my java projects. i used the
following coding...;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static
different output trying to execute same java code
different output trying to execute same java code i am using net beans 7 ide and java 6 to develop my java projects. i used the
following coding...;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static
Thread Priorities
the execution schedule of threads .
Thread gets the ready-to-
run state
according...
thread for execution. On the other hand,
if
two threads of the same priority... usually applies one of the
two following strategies:
Preemptive
Thread
Thread What is multi-threading? Explain
different states of a
thread.... At this point, the
thread is considered not alive.
Runnable (Ready-to-
run) state... ? A
thread can be considered dead when its
run() method completes. If any
thread comes