Create a scheduler task in 5 simple steps – java

Most people don’t know how to run some specific task at specific time in java. For example send daily email at 11:59 or do a daily wind up job or weakly database clean up job, etc. So here you will find how to schedule task in java in 5 simple steps.

Before starting first you need to download quartz jar. You can download it from here.

Step-1 Make a class that implements Job interface from quartz jar and implement it’s execute method. In this method define your task that will be executed on time interval.

public class ScheduleTaskHelper  implements Job  {
      public void execute(JobExecutionContext arg0) throws JobExecutionException {
           // define your task here.
      }
}

Step-2 Create another class which will create and start scheduler for this task.

public class ScheduleTask{
       public ScheduleTask()throws Exception{
       }
}

Step-3 To start scheduler task we first need to define a SchedulerFactory and a Scheduler. Following code will do that:

SchedulerFactory sf=new StdSchedulerFactory();
Scheduler sched=sf.getScheduler();

Step-4 Start that scheduler which is done by calling start method of Scheduler as follows:

sched.start();

Step-5 Create a Job of our task, a cron trigger to tell on which time interval it will run and schedule that job in the scheduler.

JobDetail jd=new JobDetail("myjob",  Scheduler.DEFAULT_GROUP,ScheduleTaskHelper.class);
CronTrigger tr=new CronTrigger("myCronTrigger", Scheduler.DEFAULT_GROUP,"0 59 23 ? * *");
sched.scheduleJob(jd, tr);

Note: – Here the string “0 59 23 ? * *” is used to define scheduler time that is on which time our scheduled task will run. Here this string will schedule task that will run daily in 11:59 pm. To know how to write appropriate cron trigger string refer to this.

Make JFrame transparent

How to create a slider for JFrame transparency and make JFrame transparent accordingly?

First create a frame that has a slider in it which will be used to set transparency amount.

import javax.swing.JFrame;
import javax.swing.JSlider;

public class TransparentFrame extends JFrame {
     public TransparentFrame() {
         setTitle("Transparent Frame");
         setSize(400,400);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         JSlider slider = new JSlider(JSlider.HORIZONTAL);
         add(slider);
         setVisible(true);
     }
     public static void main(String[] args) {
         new TransparentFrame();
     }
}

Output of this will be:

Now add a change listener to slider so we can monitor it.


slider.addChangeListener(new ChangeListener() {
       @Override
       public void stateChanged(ChangeEvent e) {
       }
});

Now we will write our transparency logic in this method but before we do that first let’s see how to make a JFrame transparent.

To make a JFrame transparent java has a built in utility class that is AWTUtilities. By using methods provided in this class we can make our JFrame transparent. Following is the code for that:

       AWTUtilities.setWindowOpacity(window, floatOpacity);

Arguments:

                  Window – Your frame/window object.

                  floatOpactity – between 0 to 1.  1 for no opacity and 0 for fully transparent.

So now we know that we have to add this logic to slider change event and give sliders value as floatOpacity value. So for that change stateChanged() method with following:

@Override
public void stateChanged(ChangeEvent e) {
      JSlider slider = (JSlider) e.getSource();
      if(!slider.getValueIsAdjusting()){
            AWTUtilities.setWindowOpacity(TransparentFrame.this, slider.getValue());
      }
}

Think it’s done. No we still have to make sure that opacity value doesn’t go beyond its limit that is 0.0f to 1.0f. So for that we have to limit our slider to these values. As slider don’t supports point values we will take values in multiple of 10 and then divide them by 100 to get desired value. For this we will change JSlider declaration and stateChanged like follow:


JSlider slider = new JSlider(JSlider.HORIZONTAL, 10, 100, 100);

Change following line in stateChanged method:


AWTUtilities.setWindowOpacity(TransparentFrame.this, slider.getValue()/100f);

So now when we run this program we see a frame with a slider in it which is set to end. And when we change slider the frame accordingly change its transparency.

Output:-

Note:-

To use AWTUtilities class in eclipse you need to change preference setting or you may have error for accessing restricted classes. To change settings do as follows:

  1. Right click on your project. Select properties.
  2. Select Java compiler and expand it.
  3. Select Errors/Warnings.
  4. Enable project specific settings.
  5. In Deprecated and Restricted API you will find Forbidden References (access rules.) Change it to Warning or Ignore.
Reference:

Also take a look take: Transparent JFrame using JNA

%d bloggers like this: