Results 1 to 6 of 6

Thread: Creating a counter in java

  1. #1
    Join Date
    Mar 2010
    Posts
    191

    Creating a counter in java

    Hello,
    I am new in JAVA and I wanted to make a counter. I come from VB and VB.NET in the IDE and there is a Timer control. I have found no equivalent in Java. I have tried this till now
    Code:
    import javax.swing.timer
    So, if any one can help me with this it would be great. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Creating a counter in java

    Hello,
    Are you sure you are using this.timer? and not this.Timer1? Moreover, the Timer class from swing is more for graphics applications, if you want a simple timer uses rather java.util.Timer. The setDelay swing timer specifies the time between different events handled by ActionListener. If you just want a time, java.util.Timer. It allows you (via the method schedule) to specify what events should happen at what time.

  3. #3
    Join Date
    Nov 2009
    Posts
    330

    Re: Creating a counter in java

    Hello,
    I think you can use the following class to do the same, that is what you require in this case
    This is the class to be used
    Code:
    java.lang.Thread
    and here is the code
    Code:
    try {
       Thread.sleep(time in milliseconds);
    }catch(InterruptedException e){
       System.out.System.out.println("Interrupted by another thread");
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Creating a counter in java

    Hello,
    If you stay with the Timer Swing (there was also java.util.Timer (non-swing) and you can also do this with a simple Thread), You should use:
    Here is the code of what I am trying to say
    Code:
    timer.setRepeats(true);
    but it is normally the default behavior. Can you show us the code of the ActionListener you've got your associates Timer plait and the entirety of your initialization step please? Hope this will help you.

  5. #5
    Join Date
    Nov 2009
    Posts
    359

    Re: Creating a counter in java

    Hello,
    Take a look at the following code, I think this may help you
    Code:
    Public class Test {
    
      Public static void hand(String ... args) {
         SwingUtilities.invokeLater(new Runnable() {
            Public void run() { 
               / / final Calendar calendar = Calendar.getInstance ();
               Final DateFormat frm = DateFormat.getTimeInstance(DateFormat.LONG);
    
               Final javax.swing.Timer tm = new javax.swing.Timer(1000, new ActionListener() {
           
                 Public void actionPerformed(ActionEvent event) {
                    long lg = System.currentTimeMillis();
                    / / calendar.setTimeInMillis (lg);
                    / / Date d = calendar.getTime ();
                    / / In case of error, use commented lines instead.
                    Date d = new Date(lg);
                    String out = frm.frm(d);
                    System.out.System.out.println(out);
                 }
               });
               / / Initialize and configure but.
               Final JToggleButton but = new JToggleButton("Start");
               but.addActionListener(new ActionListener() {
     
                 Public void actionPerformed(ActionEvent event) {
                    if (but.isSelected()) { 
                      but.setText("Stop");
                      tm.restart();
                    }
                    else { 
                      but.setText("Start");
                      tm.stop();
                    }
                 }
               });
               / / Initialize and show jfrm.
               JFrame jfrm = new JFrame("Test");
               jfrm.add(but);
               jfrm.setSize(500, 400);
               jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               jfrm.setVisible(true);
            }
         });
      }
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    333

    Re: Creating a counter in java

    Hello,
    Be careful though if you continue later with java.util.Timer, It does not run in the EDT, but in a Thread Besides, we must be careful in the changes of Swing components. In general, preferring javax.swing.Timer whether to tweak the GUI. If you need more information on the timer class in java then you can visit the official site of sun and find the api there and search for what ever class and method you need.

Similar Threads

  1. Creating a Bar Graph In Java
    By Aakarshan.d in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 12:10 PM
  2. Creating a Popup Menu in java
    By Adrina_g in forum Software Development
    Replies: 4
    Last Post: 25-02-2010, 10:29 PM
  3. Creating a Borderless Window in Java
    By Henryosa in forum Software Development
    Replies: 5
    Last Post: 24-02-2010, 10:42 PM
  4. Creating Java games for Nokia on PC
    By Zaafir in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 03:07 PM
  5. Creating Tables in Java
    By RockOn in forum Software Development
    Replies: 3
    Last Post: 09-04-2009, 10:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,568,684.70562 seconds with 17 queries