Tabnine Logo
Timer
Code IndexAdd Tabnine to your IDE (free)

How to use
Timer
in
java.util

Best Java code snippets using java.util.Timer (Showing top 20 results out of 16,650)

Refine searchRefine arrow

  • Window
  • JFrame
  • Container
  • Component
  • JComponent
origin: pedrovgs/AndroidWiFiADB

 public void run() {
  new Timer().schedule(new TimerTask() {
   @Override public void run() {
    boolean refreshRequired = androidWifiADB.refreshDevicesList();
    if (refreshRequired) {
     updateUi();
    }
   }
  }, 0, INTERVAL_REFRESH_DEVICES);
 }
});
origin: redisson/redisson

/**
 * Schedules prune.
 */
public void schedulePrune(long delay) {
  if (pruneTimer != null) {
    pruneTimer.cancel();
  }
  pruneTimer = new Timer();
  pruneTimer.schedule(
      new TimerTask() {
        @Override
        public void run() {
          prune();
        }
      }, delay, delay
  );
}
origin: apache/storm

/**
 * Start the timer to run at fixed intervals.
 */
@Override
public void start() {
  rotationTimer = new Timer(true);
  TimerTask task = new TimerTask() {
    @Override
    public void run() {
      rotationTimerTriggered.set(true);
    }
  };
  rotationTimer.scheduleAtFixedRate(task, interval, interval);
}
origin: stackoverflow.com

      JFrame frame = new JFrame();
      frame.setLayout(new GridLayout(N, N, N, N));
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      for (int i = 0; i < N * N; i++) {
        frame.add(new RotatePanel());
      frame.pack();
      frame.setVisible(true);
private final Timer timer = new Timer(25, this);
private Image image = RotatableImage.getImage(SIZE);
private double dt = DELTA_THETA;
  this.setBackground(Color.lightGray);
  this.setPreferredSize(new Dimension(
    image.getWidth(null), image.getHeight(null)));
  this.addMouseListener(new MouseAdapter() {
  super.paintComponent(g);
  Graphics2D g2d = (Graphics2D) g;
  g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
  g2d.rotate(theta);
  g2d.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
public void actionPerformed(ActionEvent e) {
  theta += dt;
  repaint();
origin: stackoverflow.com

setLayout(new BorderLayout());
add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
   JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
System.setOut(new PrintStream(taOutputStream));
new Timer(timerDelay , new ActionListener() {
  int count = 0;
  @Override
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TextAreaOutputStreamTest());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
origin: stackoverflow.com

new Timer(TIMER_DELAY, new ActionListener() {
     rectX++;
     rectY++;
     repaint();
   } else {
     ((Timer)actEvt.getSource()).stop();
Gunman mainPanel = new Gunman();
JFrame frame = new JFrame("Gunman");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
origin: stackoverflow.com

setLayout(new BorderLayout());
add(slideContainer, BorderLayout.CENTER);
Timer myTimer = new Timer(5000, new ActionListener() {
   goodbyeLabel.setOpaque(true);
   goodbyeLabel.setBackground(Color.pink);
   slideContainer.add(goodbyeLabel);
myTimer.setRepeats(false);
myTimer.start();
SlideEg mainPanel = new SlideEg();
JFrame frame = new JFrame("SlideEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
comp.setSize(getPreferredSize());
comp.setVisible(true);
comp.setLocation(getPreferredSize().width, 0);
slideFromRight(comp, oldComponent);
return returnResult;
new Timer(SLIDE_DELAY, new ActionListener() {
origin: stackoverflow.com

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jtp.setPreferredSize(new Dimension(400, 200));
    createTab("Reds", Color.RED);
    createTab("Greens", Color.GREEN);
    createTab("Blues", Color.BLUE);
    f.add(jtp, BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
private final Timer timer = new Timer(1000, this);
private final JLabel label = new JLabel("Stackoverflow!");
private final JTabbedPane parent;
  this.icon = icon;
  this.mask = icon.color.getRGB();
  this.setBackground(icon.color);
  label.setForeground(icon.color);
  this.add(label);
  timer.start();
  this.setBackground(new Color(rnd.nextInt() & mask));
  this.icon.update(count += rnd.nextInt(8));
  this.parent.repaint();
origin: stackoverflow.com

panel.setLayout(null);
label.setSize(label.getIcon().getIconWidth(), 
       label.getIcon().getIconHeight());
panel.add( label );
JFrame frame = new JFrame();
frame.add( panel );
frame.setSize(800, 600 );
frame.setVisible( true );
Timer timer = new Timer();
final Random random = new Random();
timer.schedule( new TimerTask() {
  public void run(){
     label.setLocation( random.nextInt(800-label.getWidth()), 
              random.nextInt(600-label.getHeight()));       
origin: stackoverflow.com

Timer timer = new Timer();
  timer.schedule(new TextGeneratorTask(), 250, 250);
  jScrollPane = new javax.swing.JScrollPane();
  jTextArea = new javax.swing.JTextArea();
  setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  jScrollPane.setViewportView(jTextArea);
  getContentPane().add(jScrollPane, java.awt.BorderLayout.CENTER);
  setSize(320, 240);
  setLocationRelativeTo(null);
  Rectangle visibleRect = component.getVisibleRect();
  visibleRect.y = component.getHeight() - visibleRect.height;
  component.scrollRectToVisible(visibleRect);
  java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
      new ScrollingJTextAreaExample().setVisible(true);
origin: stackoverflow.com

  bkgrndImage = ImageIO.read(bkgrdImageURL);
  playerImage = ImageIO.read(playerImageURL);
  setPreferredSize(new Dimension(bkgrndImage.getWidth(), bkgrndImage.getHeight()));
} catch (MalformedURLException e) {
  e.printStackTrace();
Timer timer = new Timer(TIMER_DELAY, new TimerListener());
timer.start();
InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actMap = getActionMap();
for (final Direction direction : Direction.values()) {
  KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false);
    int w = playerImage.getWidth() + 4 * STEP;
    int h = playerImage.getHeight() + 4 * STEP;
    MoveIcon.this.repaint(x, y, w, h); // !! repaint just the player
JFrame frame = new JFrame("MoveIcon");
frame.getContentPane().add(new MoveIcon());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
origin: stackoverflow.com

JFrame frame = new JFrame();
final JLabel label = new JLabel("Click on me and hold the mouse button down");
label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
frame.getContentPane().add(label);
label.addMouseListener(new TimingMouseAdapter());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
  timer = new Timer(100, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      System.out.println("Mouse still pressed...");
origin: stackoverflow.com

new Timer(TIMER_DELAY, new TimerListener()).start();
InputMap inputMap = getInputMap(condition);
ActionMap actionMap = getActionMap();
for (final Direction dir : Direction.values()) {
FontMetrics fontMetrics = getFontMetrics(BG_STRING_FONT);
int w = fontMetrics.stringWidth(BACKGROUND_STRING);
int h = fontMetrics.getHeight();
  repaint();
};
AnimateExample mainPanel = new AnimateExample();
JFrame frame = new JFrame("Animate Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
origin: stackoverflow.com

private Timer mTimer1;
 private TimerTask mTt1;
 private Handler mTimerHandler = new Handler();
 private void stopTimer(){
   if(mTimer1 != null){
     mTimer1.cancel();
     mTimer1.purge();
   }
 }
 private void startTimer(){
   mTimer1 = new Timer();
   mTt1 = new TimerTask() {
     public void run() {
       mTimerHandler.post(new Runnable() {
         public void run(){
           //TODO
         }
       });
     }
   };
   mTimer1.schedule(mTt1, 1, 5000);
 }
origin: stackoverflow.com

    gui.setBorder(new EmptyBorder(2, 3, 2, 3));
    gui.add(new OvalGradientPaintSurface());
    gui.setBackground(Color.WHITE);
    JFrame f = new JFrame("Oval Gradient Paint");
    f.add(gui);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationByPlatform(true);
    f.pack();
    f.setVisible(true);
    repaint();
Timer t = new Timer(15, listener);
t.start();
Graphics2D g2 = (Graphics2D)g;
AffineTransform moveToCenter = AffineTransform.
    getTranslateInstance(getWidth()/2d, getHeight()/2d);
g2.setPaint(paint);
double y = yScale/100d;
origin: stackoverflow.com

JFrame frame = new JFrame();
frame.setTitle("Clock");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
label.setFont(label.getFont().deriveFont(25f));
label.setSize(200, 100);
frame.add(label);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
  @Override
  public void run() {
origin: stackoverflow.com

private Timer time = new Timer(5, (ActionListener) this);
int x = 0, y = 0;
  x++;
  y++;
  repaint();
  setPreferredSize(new Dimension(700, 500));
  time.start();
  JFrame frame = new JFrame("Graphics2DTest");
  frame.getContentPane().add(new Graphics2DTest());
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
origin: redisson/redisson

/**
 * Stops the watcher.
 */
public void stop() {
  if (timer != null) {
    timer.cancel();
    timer = null;
  }
}
origin: stackoverflow.com

 JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer timer = new Timer(1000, (ActionEvent e) -> {
  frame.setTitle(String.valueOf(System.currentTimeMillis()));
});
timer.setRepeats(true);
timer.start();
frame.setVisible(true);
origin: stackoverflow.com

private final Timer timer = new Timer(1000, this);
private final JLabel text = new JLabel();
  JFrame f = new JFrame();
  setTranslucency( f );
  f.setUndecorated( true );
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.setBackground(new Color(0f, 0f, 0f, 1f / 3f));
  f.add(new Translucent());
  f.pack();
  f.setVisible(true);
java.utilTimer

Javadoc

Timers schedule one-shot or recurring TimerTask for execution. Prefer java.util.concurrent.ScheduledThreadPoolExecutor for new code.

Each timer has one thread on which tasks are executed sequentially. When this thread is busy running a task, runnable tasks may be subject to delays.

One-shot are scheduled to run at an absolute time or after a relative delay.

Recurring tasks are scheduled with either a fixed period or a fixed rate:

  • With the default fixed-period execution, each successive run of a task is scheduled relative to the start time of the previous run, so two runs are never fired closer together in time than the specified period.
  • With fixed-rate execution, the start time of each successive run of a task is scheduled without regard for when the previous run took place. This may result in a series of bunched-up runs (one launched immediately after another) if delays prevent the timer from starting tasks on time.

When a timer is no longer needed, users should call #cancel, which releases the timer's thread and other resources. Timers not explicitly cancelled may hold resources indefinitely.

This class does not offer guarantees about the real-time nature of task scheduling. Multiple threads can share a single timer without synchronization.

Most used methods

  • <init>
    Creates a new timer whose associated thread may be specified to Thread#setDaemon. A daemon thread is
  • schedule
    Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. Su
  • cancel
    Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently
  • scheduleAtFixedRate
    Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. Sub
  • purge
    Removes all cancelled tasks from this timer's task queue. Calling this method has no effect on the b
  • nextId
  • scheduleImpl
  • sched
    Schedule the specified timer task for execution at the specified time with the specified period, in
  • serialNumber
  • addActionListener
  • getDelay
  • isRunning
  • getDelay,
  • isRunning,
  • restart,
  • setDelay,
  • setInitialDelay,
  • setRepeats,
  • start,
  • stop

Popular in Java

  • Creating JSON documents from java classes using gson
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • getSharedPreferences (Context)
  • getContentResolver (Context)
  • Component (java.awt)
    A component is an object having a graphical representation that can be displayed on the screen and t
  • URI (java.net)
    A Uniform Resource Identifier that identifies an abstract or physical resource, as specified by RFC
  • Map (java.util)
    A Map is a data structure consisting of a set of keys and values in which each key is mapped to a si
  • JFrame (javax.swing)
  • Loader (org.hibernate.loader)
    Abstract superclass of object loading (and querying) strategies. This class implements useful common
  • Scheduler (org.quartz)
    This is the main interface of a Quartz Scheduler. A Scheduler maintains a registry of org.quartz.Job
  • From CI to AI: The AI layer in your organization
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now