Results 1 to 9 of 9

Thread: How to do modifications of clock in Java?

  1. #1
    Join Date
    Apr 2010
    Posts
    57

    How to do modifications of clock in Java?

    I was not using the code for the latest version of Java. Bad specify what I need it again i changed the original a little bit. I had to change the original post because I found a lot of new things and lots of new questions. I'm all confused now because I started my whole project in the earlier version of java. Things i could not be found in the new version of java. I could still compile but do not want something out of date. What is the best way to make a customizable digital clock java font, size and ability to be aligned (i want to have the core). I want to see how a digital clock with seconds increases every second. Please give examples of coding. How to make a very simple method to load external text file and then run automatically at a specific time during the day and slowly scroll the text vertically, no matter how long the text file. It will act as a screen announcement that will go to, say 2:00:00 PM every day. After all the text scrolls will show the date and time. Hoping that someone will lend helping hand sooner.

  2. #2
    Join Date
    Dec 2008
    Posts
    161

    Re: How to do modifications of clock in Java?

    These warnings are only to say that the Date class is old and really should not be using those methods. The source code is perhaps best suited to the task, however, this date will be printed only once. What you have to do is create a thread that the task is repeated every second. Here's an example I wrote that the production of the date string to System.out every second:
    Code:
    import java.util.Calendar; 
    java.util.Timer import; 
    java.util.TimerTask import; 
    java.text.DateFormat import; 
    public class Test ( 
    DateFormat df; 
    public Test () ( 
    df = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM) 
    Timer timer = new Timer (); 
    timer.schedule (new DoTick (), 0, 1000); / / do it every second 
    ) 
    DoTick class extends TimerTask ( 
    public void run () ( 
    Calendar c = df.getCalendar (); 
    System.out.println (df.format (c.getTime ())); 
    c.add (Calendar.SECOND, 1); 
    df.setCalendar (c); 
    ) 
    ) 
    public static void main (String [] args) ( 
    Test t = new Test (); 
    ) 
    )

  3. #3
    Join Date
    Jan 2006
    Posts
    211

    Re: How to do modifications of clock in Java?

    After looking at the code mentioned by the "Alejandro", I thought to explain you more about it. Note the use of "Timer" and "TimerTask" classes. These changes are necessary to run the code several times. Obviously, when you get to include this in your applet, you must update the value of a label / JLabel or similar instead of printing to System.out stream, but this should give you an idea of how to get the main functionality working.

  4. #4
    Join Date
    Jan 2009
    Posts
    140

    Re: How to do modifications of clock in Java?

    It seems as if the book they are learning is out of date. When you see outdated as an error code means that the function of use has changed in recent versions of the class. I am guessing that your trying to write code a while in the Java 2 runtime on your computer.
    getHours () in java.util. Date; is considered obsolete
    If the stick above error in google the first hit you get is the sun website (the writers of Java) that gives more information about what you should be using instead.

  5. #5
    Join Date
    Jan 2009
    Posts
    143

    Re: How to do modifications of clock in Java?

    To answer your first question, here is a simple Java application (not an applet) which displays the date in a JLabel, updated every second:
    Code:
    import java.awt.BorderLayout; 
    import java.awt.Font; 
    import java.text.DateFormat; 
    import java.util.Calendar; 
    import java.util.Timer; 
    import java.util.TimerTask; 
    import javax.swing.JFrame; 
    import javax.swing.JLabel; 
    public class Test extends JFrame ( 
    DateFormat df; 
    JLabel timeLabel; 
    Timer timer; 
    public Test () ( 
    df = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM) 
    timer = new Timer (); 
    timer.schedule (new DoTick (), 0, 1000); // update time every second 
    timeLabel = new JLabel ("", JLabel.CENTER) 
    timeLabel.setFont (new Font ("Comic Sans MS", Font.Bold, 32)); 
    getContentPane (). add (timeLabel, BorderLayout.CENTER); 
    setBounds (150,150,600,600); 
    setTitle ("Java Clock Test"); 
    setVisible (true); 
    ) 
    DoTick class extends TimerTask ( 
    public void run () ( 
    Calendar c = Calendar.getInstance (); 
    timeLabel.setText (df.format (c.getTime ())); 
    ) 
    ) 
    public static void main (String [] args) ( 
    Test t = new Test (); 
    ) 
    )

  6. #6
    Join Date
    Apr 2010
    Posts
    57

    Re: How to do modifications of clock in Java?

    Thanks a lot to everyone for helping me out. That was exactly what I needed. But i got a question. I tried to change the font color and background color, add:
    Code:
     timeLabel.setColor (Color.Blue)
    and I can not change the background color. Could you tell me how to do this??

  7. #7
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to do modifications of clock in Java?

    You have to try the following things :
    • SetOpaque (true);
    • (SetForeground new Color (0). White), // to change the font color
    • SetBackground (new Color (0). Red), // to change the background color

    Without setOpaque foreground and background color does not change.

  8. #8
    Join Date
    Jan 2010
    Posts
    112

    Re: How to do modifications of clock in Java?

    My problem is I do not know how to load a text from a text file (that's the easy part) and then loaded into something (I do not know what I load) and automatically scroll, type of them as the closing credits of the film. I want everyone in a method, so that at any given time during the day (using the clock code in my previous post) and the clock will run until there are no ads to display them all. Then, return to the clock. Who can help?

  9. #9
    Join Date
    Nov 2008
    Posts
    996

    Re: How to do modifications of clock in Java?

    Loading text file is easy:
    Code:
     public String loadFile (String filename) ( 
    try ( 
    BufferedReader br = new BufferedReader (new FileReader (filename)); 
    StringBuffer data = new StringBuffer (); 
    String temp; 
    while ((temp = br.readLine ())! = null) ( 
    data.append (temp); 
    ) 
    br.close (); 
    data.toString return (); 
    ) 
    catch (Exception ex) () 
    return null; 
    )

Similar Threads

  1. Replies: 6
    Last Post: 05-07-2011, 10:24 AM
  2. 24-Hour Clock Timer Task in Java
    By Kohlmann in forum Software Development
    Replies: 6
    Last Post: 07-09-2010, 10:40 PM
  3. printing modifications
    By twilltwo in forum Windows Software
    Replies: 4
    Last Post: 03-02-2010, 03:57 AM
  4. Replies: 3
    Last Post: 22-07-2009, 09:14 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,928,086.78576 seconds with 16 queries