Results 1 to 6 of 6
- 06-27-2012, 05:41 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
Trying to create a program that beeps after a user-specified interval using a gui
hello everyone,
I tried to make the program by making a JFrame with 2 JTextFields. the first one takes in how many seconds between each beep the user wants, and the second one was supposed to update the amount of seconds that have passed iteratively. Im having trouble with the second JTextField, because I dont know how to have it continuously update with the new "seconds" variable. Heres my code so far:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
public class beeper extends JFrame {
private JTextField counter;
private JTextField inputTime;
private int seconds;
private static int interval;
public beeper() {
super("Interval Beeper");
counter = new JTextField("Counter goes here.");
add(counter, BorderLayout.SOUTH);
inputTime = new JTextField("Length of interval between beeps.");
add(inputTime, BorderLayout.NORTH);
handler hand = new handler();
//counter.addActionListener(hand);
inputTime.addActionListener(hand);
}
public class handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
String string = e.getActionCommand();
interval = Integer.parseInt(string);
int initInterval = interval;
JOptionPane.showMessageDialog(null, String.format("The interval you want is %s seconds", string));
seconds = 0;
for (int i = 3; i < 10; i++) {
try {
Thread.sleep(1000);
seconds++;
counter.setText(String.format("%d second", seconds));
if (seconds== interval){
Toolkit.getDefaultToolkit().beep();
interval+= initInterval;
}
} catch (InterruptedException ev) {
ev.printStackTrace();
}
}
counter.setText("Time expired");
}
}
}
and Ive got a pretty typical main class that sets the visibility and default close and whatnot. Thanks for all your help.
- 06-27-2012, 05:48 PM #2
Re: Trying to create a program that beeps after a user-specified interval using a gui
Please use code tags on posted code:
BB Code List - Java Programming Forum
You do NOT want to sleep in an action listener that is executed on Swing's EDT. That will hang your GUI responses. Look at starting a Timer to do the "wait" until the future event should happen.Last edited by Norm; 06-27-2012 at 05:51 PM.
If you don't understand my response, don't ignore it, ask a question.
-
Re: Trying to create a program that beeps after a user-specified interval using a gui
Suggestions:
- Don't use Thread.sleep(...) on the Swing event thread, not unless you want to put the whole GUI to sleep.
- Instead use a Swing Timer (javax.swing.Timer) to do this for you.
- Simply set the text of your text component inside of the timer's ActionListener.
- When posting code here, you'll want to use [code] [/code] tags so that your code is readable.
- Read up on and follow Java naming conventions: classes including internal classes all start with a capital letter. This becomes especially important if you ask others to try to read and understand your code either for the purpose of helping you or evaluating you.
Luck and welcome to the forum!
- 06-27-2012, 05:56 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 28
Re: Trying to create a program that beeps after a user-specified interval using a gui
Hard to tell from that code, since it isn't in
Java Code:tags
I recommend going through the Swing Concurrency tutorial.
ETA: Bah!Please do not ask for code as refusal often offends.
** This space for rent **
- 06-27-2012, 08:18 PM #5
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
Re: Trying to create a program that beeps after a user-specified interval using a gui
Sorry about the lack of coding tags :/ But Thanks so much Fubarable, these were fantastically helpful points.
-
Re: Trying to create a program that beeps after a user-specified interval using a gui
You're welcome, and again welcome to Java-Forums.org!
Similar Threads
-
Need help with weighted interval scheduling problem.
By chris1 in forum Advanced JavaReplies: 3Last Post: 11-08-2013, 04:44 PM -
how to create table for each user
By abhi7080 in forum New To JavaReplies: 3Last Post: 01-04-2012, 07:42 AM -
Easiest way to detect beeps in audio file
By bill17 in forum New To JavaReplies: 0Last Post: 11-26-2011, 04:46 PM -
API to create user accounts
By amitnme in forum Advanced JavaReplies: 1Last Post: 02-05-2010, 07:08 PM -
need info on running thread during a particular time interval alone
By karthikeyan_raju in forum Threads and SynchronizationReplies: 2Last Post: 10-06-2009, 02:40 AM
Bookmarks