Vibrate a WindowTag(s): AWT

This HowTo is based on this post :

This can be useful to make a visual effect when an event is occuring.

import java.awt.*;

public class FrameUtils {

  private final static int VIBRATION_LENGTH = 20;
  private final static int VIBRATION_VELOCITY = 5;
  private FrameUtils() { }
  public static void vibrate(Frame frame) { 
    try { 
      final int originalX = frame.getLocationOnScreen().x; 
      final int originalY = frame.getLocationOnScreen().y; 
      for(int i = 0; i < VIBRATION_LENGTH; i++) { 
        frame.setLocation(originalX, originalY + VIBRATION_VELOCITY); 
        frame.setLocation(originalX, originalY - VIBRATION_VELOCITY);
        frame.setLocation(originalX + VIBRATION_VELOCITY, originalY);
        frame.setLocation(originalX, originalY); 
    catch (Exception err) { 
To use it, simply pass a Frame to the vibrate method.
To make it more Swing-oriented, you change the method signature for a JFrame instead.
Here an example.

We display a small window. When a file is added or deleted, the window is shaking for a brief moment and display the event. By default, the folder c:/temp is used but you can specify another one on the command line.

First get the code to detect a file modfication in a folder (in this HowTo, you need the DirWatcher and DirFilterWatcher classes). Plus the following classes.

import java.awt.*;
import java.awt.event.*;

public class DirWatchWindow extends Frame {
  Label folder;
  Label info;
  public DirWatchWindow() { 
    setSize(600, 100); 
    setLayout(new BorderLayout());
    folder = new Label("");
    info   = new Label("");
    add(folder, BorderLayout.NORTH);
    add(info, BorderLayout.SOUTH);
    (new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
  public void setInfo(String text) {
  public void setFolder(String dir) {
    folder.setText("Watching folder : " + dir);

import java.util.*;

public class DirWatchTest {

  public static void main(String args[]) {
    String folderToWatch = "c:/temp";

    if (args.length > 0) {
      folderToWatch = args[0];
    final DirWatchWindow dww = new DirWatchWindow();
    TimerTask task = new DirWatcher(folderToWatch, "txt" ) {
      protected void onChange( File file, String action ) {
        // here we code the action on a change
        dww.setInfo("File : "+ file.getName() +"  action: " + action);

    Timer timer = new Timer();
    timer.schedule( task , new Date(), 1000 );
You can download an executable JAR here

To launch the demo

java -jar DirWatch.jar  
to watch the default directory c:/temp or
java -jar DirWatch.jar  c:/myfolder
to specify your own folder. This demo is watching for files with the extension txt only.
blog comments powered by Disqus