1 2 Previous Next 22 Replies Latest reply: Oct 12, 2007 3:28 PM by 807600 RSS

    Turn a button into a date and a clock

    807600
      I've been able code it so when you click the button on the program it will then change to show you a date. Sadly I lost my code and now I can not even remember what I did.

      My second problem is that I have the same type of scenario but this time I have a button and a text field. When I click the button a self updating date should pop up in the text field

      EDIT:

      btw all this has to be contained in a frame

      Edited by: Hessmix on Oct 12, 2007 1:20 PM
        • 1. Re: Turn a button into a date and a clock
          807600
          On your first problem, everybody loses code a couple times before they realize that they need to backup their files in 2 or 3 places frequently.

          On your second problem, i may not be as good of a source as some of the more advanced and elder members of the forum, but here's the steps i'd take to tackle your problem:

          1) establish an event listener for the click of the button
          2) create and display a current date through the java.util.date API (and format it correctly)
          3) create a separate thread to update the date and refresh the text field whenever the date/time changes

          Edited by: redfalconf35 on Oct 12, 2007 1:24 PM
          • 2. Re: Turn a button into a date and a clock
            807600
            You've come to the right place, this is www.pleasedomyhomework.com
            I'd do it, then I'd have the entire forum on my back, again ;)

            What have you coded so far? Paste your code, use CODE tags...

            Edited by: __colin on Oct 12, 2007 2:24 PM
            • 3. Re: Turn a button into a date and a clock
              807600
              Hessmix wrote:
              I've been able code it so when you click the button on the program it will then change to show you a date. Sadly I lost my code and now I can not even remember what I did.
              Sadly, you'll have to recode it. If you've done it before, I am confident that you can code it again. Good luck.
              • 4. Re: Turn a button into a date and a clock
                807600
                /*
                 * Shawn
                 * 9/6/07
                 * 
                 */
                
                import javax.swing.*;
                import java.awt.*;
                import java.awt.event.* ;
                import javax.swing.event.*;
                import java.text.DateFormat.*;
                import java.text.SimpleDateFormat.*;
                import java.util.Random.*;
                import java.util.Date.*;
                import java.text.*;
                import java.util.*;
                import java.awt.Event.*;
                
                     public class DateProblem extends JFrame 
                     {
                          static JButton ButtonOne = new JButton();
                          SimpleDateFormat sdf = new SimpleDateFormat("MMM.dd.yyyy");
                         Date today = new Date();
                      
                           public static void main(String args[])
                           {
                                // Frame
                              new DateProblem().show();
                           }
                           
                           public DateProblem() 
                           {
                                setTitle("");
                
                              getContentPane().setLayout(new GridBagLayout());
                               GridBagConstraints gridConstraints = new GridBagConstraints();
                               ButtonOne.setText("Click Me");
                               gridConstraints.gridx = 0;
                               gridConstraints.gridy = 0;
                               getContentPane().add(ButtonOne, gridConstraints);
                               ButtonOne.addActionListener(new ActionListener() 
                                    
                               {
                                     public void actionPerformed(ActionEvent e) 
                                     {
                                       ButtonOneActionPerformed(e);
                                     }
                              });
                               
                           }
                     private void ButtonOneActionPerformed(ActionEvent e)
                       {
                         ButtonOne.setText(sdf.format(today));
                       }
                }
                This is for first one but the code is wrong.

                Edited by: Hessmix on Oct 12, 2007 1:29 PM


                And this is the self updating clock
                /*
                 *
                */
                
                import javax.swing.*;
                import java.awt.*;
                import java.awt.event.* ;
                import javax.swing.event.*;
                import java.text.DateFormat.*;
                import java.text.SimpleDateFormat.*;
                import java.util.Random.*;
                import java.util.Date.*;
                import java.text.*;
                import java.util.*;
                import java.awt.Event.*;
                
                public class calendar extends JFrame 
                {
                     SimpleDateFormat sdf = new SimpleDateFormat("MMM.dd.yyyy");
                    Date date = new Date(); 
                         
                  // declare controls used
                  static JButton startButton = new JButton();
                  static JButton exitButton = new JButton();
                  static JTextField startTextField = new JTextField();
                  
                  public static void main(String args[]) 
                  {
                    new calendar().show();
                  }
                
                  public calendar() 
                  {
                    // frame constructor
                    setTitle("Stopwatch Application");
                    addWindowListener(new WindowAdapter() 
                    {
                      public void windowClosing(WindowEvent e) 
                      {
                        exitForm(e);
                      }
                    });
                    getContentPane().setLayout(new GridBagLayout());
                
                    // add controls
                    GridBagConstraints gridConstraints = new GridBagConstraints();
                    startButton.setText("Click for the Date");
                    gridConstraints.gridx = 0;
                    gridConstraints.gridy = 0;
                    getContentPane().add(startButton, gridConstraints);
                    startButton.addActionListener(new ActionListener() 
                    {
                      public void actionPerformed(ActionEvent e) 
                      {
                        startButtonActionPerformed(e);
                      }
                    });
                
                    exitButton.setText("Exit");
                    gridConstraints.gridx = 0;
                    gridConstraints.gridy = 2;
                    getContentPane().add(exitButton, gridConstraints);
                    exitButton.addActionListener(new ActionListener() 
                    {
                      public void actionPerformed(ActionEvent e) 
                      {
                        exitButtonActionPerformed(e);
                      }
                    });
                
                    startTextField.setText("");
                    startTextField.setColumns(15);
                    gridConstraints.gridx = 2;
                    gridConstraints.gridy = 0;
                    getContentPane().add(startTextField, new GridBagConstraints());
                  }
                  
                  private void startButtonActionPerformed(ActionEvent e)
                  {
                    // click of start timing button
                    
                    startTextField.setText(String.valueOf(sdf.format(date)));
                  }
                  
                  private void exitButtonActionPerformed(ActionEvent e)
                  {
                    System.exit(0);
                  }
                  
                  private void exitForm(WindowEvent e)
                  {
                    System.exit(0);
                  }
                
                }
                Edited by: Hessmix on Oct 12, 2007 1:31 PM

                Edited by: Hessmix on Oct 12, 2007 1:32 PM
                • 5. Re: Turn a button into a date and a clock
                  807600
                  Hessmix wrote:
                  This is for first one but the code is wrong.
                  Ok what is wrong with the first code? What doesn't/does it do?

                  As for your second question, you said it only needs to update when you click the button? Then surely you don't need to be running a second thread? However if it needs to be a "ticking clock" then you can use the Timer class.

                  From your SimpleDateFormatter I can tell you'd probably not see anything "work" anyway, because you're not using Hours/Minutes/Seconds?
                  • 6. Re: Turn a button into a date and a clock
                    807600
                    I'm using JCreator and when I choose to compile it, it says process completed but no frame pops up.
                    • 7. Re: Turn a button into a date and a clock
                      807600
                      __colin wrote:


                      However if it needs to be a "ticking clock" then you can use the Timer class.
                      Ya know, i had a feeling that java had an automatic way to update a clock, but when i had to code one i was too lazy to look. It ended up taking me 100+ lines of code to do what could probably be done in 5... just goes to show you how helpful the API's can be

                      (srry about the off-topic)
                      • 8. Re: Turn a button into a date and a clock
                        807600
                        Hmm, I am running your code exactly how you pasted it and it works fine.

                        Give the frame a size too.
                        • 9. Re: Turn a button into a date and a clock
                          807600
                          try adding a setSize command for the frame. I found the frame, but it was miniscule
                          • 10. Re: Turn a button into a date and a clock
                            807600
                            redfalconf35 wrote:
                            __colin wrote:


                            However if it needs to be a "ticking clock" then you can use the Timer class.
                            Ya know, i had a feeling that java had an automatic way to update a clock, but when i had to code one i was too lazy to look. It ended up taking me 100+ lines of code to do what could probably be done in 5... just goes to show you how helpful the API's can be

                            (srry about the off-topic)
                            Hehehe yeah java is kind of gay like that, nothing is "automatic" with it :(

                            100+ lines of code for that? I'd cry ;)
                            • 11. Re: Turn a button into a date and a clock
                              807600
                                  public static void main(String args[])
                                  {
                                      // Frame
                                      DateProblem dp = new DateProblem();
                                      dp.pack();  // don't forget to pack your frame
                                      dp.setVisible(true);  // and show it
                                      
                                      // **** don't use deprecated methods
                                      //new DateProblem().show();  
                                  }
                              • 12. Re: Turn a button into a date and a clock
                                807600
                                redfalconf35 wrote:
                                try adding a setSize command for the frame. I found the frame, but it was miniscule
                                He needs to "pack" it.
                                • 13. Re: Turn a button into a date and a clock
                                  807600
                                  Oh and I just checked in my IDE which I missed in the code, please add a "setDefaultCloseOperation" so that you don't have the process running in the background once you try close it
                                  • 14. Re: Turn a button into a date and a clock
                                    807600
                                    __colin wrote:
                                    100+ lines of code for that? I'd cry ;)
                                    i may have exaggerated a bit, but that doesn't take away from the fact that my code is really bulky and clumsy (comes from having only 6 or 7 weeks of java experience and teaching myself)

                                    @petes1234 - is packing something that is involved in Swing, or is it AWT as well? I've only been using awt, and have had very limited exposure to packing

                                    Edited by: redfalconf35 on Oct 12, 2007 1:47 PM
                                    1 2 Previous Next