David Landup
David Landup

Reputation: 168

Do Java Swing action on specified Date

I'm trying to create a program which has two options: - Do specific task after countdown (in minutes). - Do specific task on the date selected. Both are using jSpinners however, I don't know how to do the action on the specific date, here's the code below, thank you in advance!

protected Object doInBackground() throws Exception {
           Timer t = new Timer(0,  new ActionListener() {
               public void actionPerformed(ActionEvent e) {
               }
           });

           //Checking state of CheckBoxes (one cancels the other)
               if(jCheckBox2.isSelected()) {
               try {
                   int delay =(int) jSpinner2.getValue();
                   jCheckBox1.setSelected(false);
                       Thread.sleep(delay*60000); //To delay the code from miliseconds to minutes
                   } catch (InterruptedException ex) {
                       Logger.getLogger(App_Gui.class.getName()).log(Level.SEVERE, null,
   ex);
                   }
           }
               else if (jCheckBox1.isSelected()) {
                   Date delay2 = (Date) jSpinner1.getValue();
                   jCheckBox2.setSelected(false);
                   Thread.sleep(delay2); //What should I put here instead of Thread.sleep()???????

       }

           //If all is right, start the timer
           t.start();       
        t.setRepeats(false);
           //Popup dialog
           JDialog dialog = new JDialog();     
          dialog.setLocation(700, 300);
          dialog.setSize(600, 400);  
          dialog.setVisible(true);
          //Speed of color changing
          try {

             Thread.sleep(jSlider1.getValue());
                     } catch (InterruptedException ex) {
               Logger.getLogger(App_Gui.class.getName()).log(Level.SEVERE, null,
   ex);
           } //Setting the color
          dialog.getContentPane().setBackground(jLabel2.getBackground());

        dialog.setModal(true);
        Assignment_Tajmer_Aplikacija.f.setVisible(false);    

        return null;
       }
       protected void done() {
           System.out.println("Done!");
       }
        };
        sw.execute();



       }                                        

       private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {                                      
           // TODO add your handling cosadde here:
           jSlider1.addChangeListener(new ChangeListener() {
         public void stateChanged(ChangeEvent e) {
           JSlider source = (JSlider) e.getSource();
           //System.out.println(source.getValue());

         }
       });
       }

Upvotes: 0

Views: 61

Answers (1)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

You have 2 choices:

  1. You compute time difference in milliseconds between current date and target date. So you can start the swing timer. Something like myDate.getTime() - Sytem.currentTimeMillis() (it's probably not always correct)
  2. You use a Library which do it for you (for example Quartz). In this case you need to synchronize your job with the Swing thread using the method SwingUtilities.invokeLater(Runnable).

And never use Thread.sleep() in a Swing application. Start a timer instead.

Upvotes: 2

Related Questions