Vetri Selvan
Vetri Selvan

Reputation: 25

Need to trigger below Java swing application one hour once

I have created one task bar .But i want to display this taskbar one hour once when the user click on Reminder me button and the task bar should close and again open after one hour.Please find the below code .

Kindly help me on this please!!

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class RadioButtons {

public static void main(String[] args) {


    Radiobutton r=new Radiobutton();

}
}

class Radiobutton extends JFrame implements ActionListener
{
JButton b1,b2;
JLabel l1,l2;
public Radiobutton()
{
    l1=new JLabel("Pending");
    l2=new JLabel("Pending");
    b1=new JButton("AVM DART");
    b2=new JButton("HANDSHAKE");
    add(b1);
    add(l1);
    add(b2);
    add(l2);
    b1.setBackground(Color.RED);
    b2.setBackground(Color.RED);
    b1.setPreferredSize(new Dimension(200, 50));
    b2.setPreferredSize(new Dimension(200, 50));
    b1.addActionListener(this);
    b2.addActionListener(this);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
    Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
    int x = (int) rect.getMaxX() -300;
    int y = 500;
    setLocation(x, y);
    setLayout(new FlowLayout());
    setVisible(true);
    setSize(300, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if(e.getSource()==b1)
    {
        if (((Component)source).getBackground().equals(Color.GREEN))
        {
            ((Component)source).setBackground(Color.RED);
            l1.setText("Pending");
        } 
        else 
        {
            ((Component)source).setBackground(Color.GREEN);
            l1.setText("   Done");
        }

    }
    if(e.getSource()==b2)
    {
        if (((Component)source).getBackground().equals(Color.GREEN))
        {
            ((Component)source).setBackground(Color.RED);
            l2.setText("Pending");

        } else 
        {
            ((Component)source).setBackground(Color.GREEN);
            l2.setText("   Done");
        }
    }


    }

}

Upvotes: 0

Views: 98

Answers (1)

Rishabh Kumar
Rishabh Kumar

Reputation: 557

I assume the class RadioButton is your taskbar that you want to popup once every hour. "ScheduledExecutorService" interface can do your job here.

Example:

public class RadioButtons 
{
    // This is the Scheduler that we will execute every hour
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

   public static void main(String[] args) 
   {
      // instead of instantiating it here, we will do this in a thread.
      //Radiobutton r = new Radiobutton();

      final Runnable beeper = new Runnable() 
      {
         public void run() 
         { 
            // do anything here that you want to execute in a scheduler.
            Radiobutton r=new Radiobutton();
         }
      };

      //finally we schedule the scheduler to execute Runnable every hour.
      final ScheduledFuture<?> beeper = scheduler.scheduleAtFixedRate(beeper, 0, 1, TimeUnit.HOURS);

   }
}

Note: Your frame will start as soon as the program starts and then after every hour. Also if you don't close the frame, then after an hour a new frame will open (the old frame will not close until someone closes it manually).

You can read more about ScheduledExecutorService and it's method scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) at https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html

Upvotes: 1

Related Questions