Results 1 to 6 of 6
  1. #1
    merrymac is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default 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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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!

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default 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
    , but I'm going to hazard a guess you're sleep()ing the EDT (the Swing thread).
    I recommend going through the Swing Concurrency tutorial.

    ETA: Bah!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    merrymac is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Need help with weighted interval scheduling problem.
    By chris1 in forum Advanced Java
    Replies: 3
    Last Post: 11-08-2013, 04:44 PM
  2. how to create table for each user
    By abhi7080 in forum New To Java
    Replies: 3
    Last Post: 01-04-2012, 07:42 AM
  3. Easiest way to detect beeps in audio file
    By bill17 in forum New To Java
    Replies: 0
    Last Post: 11-26-2011, 04:46 PM
  4. API to create user accounts
    By amitnme in forum Advanced Java
    Replies: 1
    Last Post: 02-05-2010, 07:08 PM
  5. need info on running thread during a particular time interval alone
    By karthikeyan_raju in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-06-2009, 02:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •