Results 1 to 7 of 7
  1. #1
    vkokaram is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Post need help with low priority thread to display time

    The below displays time in a window and I just want the time to update every minute or whatever is easiest if another method.


    Test4.java
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Test4 extends JFrame  {
        
        public Test4() {
            super("Test Toolkit");
            setSize(700, 200);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            FlowLayout beginButtons = new FlowLayout();
            setLayout(beginButtons);
            JLabel startLabels = new JLabel("Current System");
            TimePanel time = new TimePanel();
    
            add(startLabels);
    
            
            add(time);
    
    
            setVisible(true);
            }
            
        public static void main(String[] args) {
            Test4 StartWindow = new Test4();
        }
    }



    TimePanel.java
    Java Code:
    import javax.swing.*;
    import java.util.*;
    
    public class TimePanel extends JPanel {
        public TimePanel() {
            super();
            String currentTime = getTime();
            JLabel time = new JLabel("Time: ");
            JLabel current = new JLabel(currentTime);
            add(time);
            add(current);
        }
        String getTime() {
            String time;
            // get current time and date
            Calendar now = Calendar.getInstance();
            int hour = now.get(Calendar.HOUR_OF_DAY);
            int minute = now.get(Calendar.MINUTE);
            int month = now.get(Calendar.MONTH);
            int day = now.get(Calendar.DAY_OF_MONTH);
            int year = now.get(Calendar.YEAR);
    
            String monthName = "";
            switch (month) {
                case (1):
                    monthName = "January";
                    break;
                case (2):
                    monthName = "February";
                    break;
                case (3):
                    monthName = "March";
                    break;
                case (4):
                    monthName = "April";
                    break;
                case (5):
                    monthName = "May";
                    break;
                case (6):
                    monthName = "June";
                    break;
                case (7):
                    monthName = "July";
                    break;
                case (8):
                    monthName = "August";
                    break;
                case (9):
                    monthName = "September";
                    break;
                case (10):
                    monthName = "October";
                    break;
                case (11):
                    monthName = "November";
                    break;
                case (12):
                    monthName = "December";
            }
            time = monthName + " " + day + ", " + year + " " + hour + ":" + minute;
                return time;
        }
    }
    Last edited by Fubarable; 07-18-2010 at 06:49 PM. Reason: code

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

    Default

    No need to directly use a Thread but rather you can use a Swing Timer, but be sure to get the actual time from the System as the Timer's accuracy is not bad but not great.

    Also, I've added code tags to your post to help make the posted code readable, and welcome to the Java-Forums.org!

  3. #3
    vkokaram is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Thank you and it looks a lot better with the tags, could you modify my example on how this would work?

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

    Default

    Quote Originally Posted by vkokaram View Post
    Thank you and it looks a lot better with the tags, could you modify my example on how this would work?
    Why not first read up on Swing Timers at the Java tutorials and first try this yourself. You'll gain so much more from the effort. You can find the tutorial here:

    How to use Swing Timers

  5. #5
    vkokaram is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Please do not bother to answer my posts since you are not will to assist, I thank you for obvious answers to my question. Next time I need to ask if it is raining I will think of you.

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

    Default

    Quote Originally Posted by vkokaram View Post
    Please do not bother to answer my posts since you are not will to assist, I thank you for obvious answers to my question. Next time I need to ask if it is raining I will think of you.
    Not to be insulting, but you will get more out of your Java education if you come here with the attitude of "please help me learn" rather than "here is my code, fix it for me". Much luck.

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

    Default

    One other suggestion: Use a SimpleDateFormat object to get your date/time String as this will simplify your code greatly and also you'd avoid the mistake you're making by assuming that Calendar months are 1-based when in fact they are 0-based (that's why your month is displaying June instead of July). e.g.,

    Java Code:
    class TimePanel extends JPanel {
       private static final String DATE_FORMAT_STR = "MMMM d, yyyy h:mm:ss a";
       private JLabel current;
       private SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_STR);
       
       public TimePanel() {
          super();
          String currentTime = getTime();
          JLabel time = new JLabel("Time: ");
          current = new JLabel(currentTime);
          add(time);
          add(current);
          
          new Timer(200, new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                //... I'll let you figure out this part.  ;)
             }
          }).start();
       }
    
       String getTime() {
          Calendar now = Calendar.getInstance();
          return sdf.format(now.getTime());
       }
    }

Similar Threads

  1. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 07:36 PM
  2. How to display date & time on GUI?
    By BluXit in forum New To Java
    Replies: 5
    Last Post: 05-26-2010, 11:33 AM
  3. Need help with Process execution time display
    By murali18 in forum New To Java
    Replies: 5
    Last Post: 03-18-2010, 11:19 PM
  4. how high-priority thread allow other thread
    By rameshkr in forum Threads and Synchronization
    Replies: 4
    Last Post: 10-15-2009, 10:46 PM
  5. How to get/set thread priority
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:40 PM

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
  •