In other words the recording continues until the stop button is press. What is need is that it stops inself after the clock gets to the number example (2000) from above
Main Topics
Browse All TopicsI need to be able to have the Thread.sleep(2000); stop after the 2000, continues to run until the stop button is pressed. This need to be able to work for all the other times I have. It starts at 2000 at 500 segments to 15000. What would be done to accomplish this?
if(r3.isSelected()) {
try {
Thread.sleep(2000);
fileType = AudioFileFormat.Type.WAVE;
audioFile = new File("silence2.wav");
}
final JButton b1 = new JButton("Capture");
final JButton b2 = new JButton(" Stop ");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
{
b1.setEnabled(false);
b2.setEnabled(true);
captureAudio();
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
{
b1.setEnabled(true);
targetDataLine.stop();
targetDataLine.close();
}
});
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi Christopher,
Study this little example:
/*
* TimerDemo.java
*/
import java.util.*;
public class TimerDemo {
/** Creates a new instance of TimerDemo */
public TimerDemo() {
System.out.println(new Date());
(new Timer()).schedule(new TimerTask() {
public void run() {
System.out.println("Hello!
System.out.println(new Date());
System.exit(0);
}
}, 5000);
}
public static void main(String[] args) {
new TimerDemo();
}
}
The output is:
Fri May 14 22:04:23 CEST 2004
Hello! 5 seconds elapsed.
Fri May 14 22:04:28 CEST 2004
I think that's the solution of your problem:
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
{
b1.setEnabled(false);
b2.setEnabled(true);
captureAudio();
(new Timer()).schedule(new TimerTask() {
public void run() {
b2.doClick(); // <<<<<<<<<<< This presses the Stop button programmatically after 2 seconds
}
}, 2000);
}
});
So, when they press "capture", the capturing starts. After X seconds the stop button is programmatically pressed, so the capturing is stopped.
> I need to be able to have the Thread.sleep(2000); stop after the 2000
This should do it :
if(r3.isSelected()) {
try {
int delay1 = 2000; // milliseconds
ActionListener updater1 = new ActionListener() {
public void actionPerformed(ActionEven
fileType = AudioFileFormat.Type.WAVE;
audioFile = new File("silence2.wav");
}
};
new Timer(delay1, updater1).start();
}
catch(Exception e) {
e.printStackTrace();
}
}
Hope that helps . . .
Javatm
When the user really presses "Stop" you have to cancel the timer.
final JButton b1 = new JButton("Capture");
final JButton b2 = new JButton(" Stop ");
Timer timer = new Timer();
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
{
b1.setEnabled(false);
b2.setEnabled(true);
captureAudio();
timer.schedule(new TimerTask() {
public void run() {
b2.doClick();
}
}, 2000);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
timer.cancel(); // <<<<<<<<<<<<<<<<<<<<<<<<<<
b1.setEnabled(true);
targetDataLine.stop();
targetDataLine.close();
}
});
I will add that to every radiobuttons time just change the time reference.
This should do it :
if(r3.isSelected()) {
try {
int delay1 = 2000; // milliseconds
ActionListener updater1 = new ActionListener() {
public void actionPerformed(ActionEven
fileType = AudioFileFormat.Type.WAVE;
audioFile = new File("silence2.wav");
}
};
new Timer(delay1, updater1).start();
}
catch(Exception e) {
e.printStackTrace();
}
}
Javatm I get this error message with adding that.
local variable fileType is accessed from within inner class; needs to be declared final
QUESTION:
I need to be able to have the Thread.sleep(2000); stop after the 2000, continues to run until the stop button is pressed. This need to be able to work for all the other times I have. It starts at 2000 at 500 segments to 15000. What would be done to accomplish this?
<<<< think that's the solution of your problem: how would this work for the other times mentioned in the question TEXT ABOVE
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
{
b1.setEnabled(false);
b2.setEnabled(true);
captureAudio();
(new Timer()).schedule(new TimerTask() {
public void run() {
b2.doClick(); // <<<<<<<<<<< This presses the Stop button programmatically after 2 seconds
}
}, 2000);
}
});
> local variable fileType is accessed from within inner class; needs to be declared final
Then declare the object final like :
final JButton b2 = new JButton(" Stop ");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
{
b1.setEnabled(false);
b2.setEnabled(true);
captureAudio();
(new Timer()).schedule(new TimerTask() {
public void run() {
b2.doClick(); // <<<<<<<<<<< This presses the Stop button programmatically after 2 seconds
}
}, 2000);
}
});
Hi Christopher,
I read your Q times and times again...
I'm not able to get what you mean.
>> I need to be able to have the Thread.sleep(2000); stop after the 2000, continues to run until the stop button is pressed.
That isn't an good english sentence, is it?
>> In other words the recording continues until the stop button is press.
So, that's how it is now.you have to press the button manually to stop the recording. Right?
>> What is need is that it stops inself after the clock gets to the number example (2000) from above
So, you want the recording to stop after 2 seconds. In another case after 2,5 seconds. And so on, till 15 seconds. Right?
Well, that's what my code does.
But apparently you want something else?
What?
No you are right. I need a lot of help with this. You are on the right track. What I though your code did was clicked the button automaticly after 2000, which it does. Where I got confused was how to get it to work with the times in the If statements Sorry about the lack of clarity about this, but I didn't get the point across and wasn't understood as well from the beginning I think. That is hard in this format. I look forward towards working with you on this question.
Business Accounts
Answer for Membership
by: Moroni24Posted on 2004-05-14 at 11:36:03ID: 11070940
I'd love to help, but your question makes absolutely no sense.... can you reword it?