Example usage for javax.swing Timer Timer

List of usage examples for javax.swing Timer Timer

Introduction

In this page you can find the example usage for javax.swing Timer Timer.

Prototype

public Timer(int delay, ActionListener listener) 

Source Link

Document

Creates a Timer and initializes both the initial delay and between-event delay to delay milliseconds.

Usage

From source file:Main.java

private void startProgressBar() {
    progressBarTimer = new Timer(TIMER_PAUSE, al);
    progressBarTimer.start();
}

From source file:Main.java

public TestPane() {
    setLayout(new BorderLayout());
    JPanel searchPane = new JPanel();
    searchPane.add(new JLabel("Find: "));

    searchPane.add(findText);//from w  w  w. j a  va 2  s. c  o  m

    add(searchPane, BorderLayout.NORTH);
    add(new JScrollPane(ta));

    try (BufferedReader reader = new BufferedReader(new FileReader(new File("c:/Java_Dev/run.bat")))) {
        ta.read(reader, "Text");
    } catch (Exception e) {
        e.printStackTrace();
    }
    ta.setCaretPosition(0);

    keyTimer = new Timer(250, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String find = findText.getText();
            Document document = ta.getDocument();
            try {
                for (int index = 0; index + find.length() < document.getLength(); index++) {
                    String match = document.getText(index, find.length());
                    if (find.equals(match)) {
                        DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(
                                Color.YELLOW);
                        ta.getHighlighter().addHighlight(index, index + find.length(), highlightPainter);
                    }
                }
            } catch (BadLocationException exp) {
                exp.printStackTrace();
            }
        }
    });
    keyTimer.setRepeats(false);

    findText.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            keyTimer.restart();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            keyTimer.restart();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            keyTimer.restart();
        }
    });
}

From source file:Main.java

public Main() {
    setLayout(new GridLayout(ROWS, COLS));
    for (int row = 0; row < grid.length; row++) {
        for (int col = 0; col < grid[row].length; col++) {
            JLabel label = new JLabel();
            int index = random.nextInt(COLORS.length);
            label.setIcon(ICONS[index]);
            add(label);//  w  w  w.  j a va  2  s .  co  m
            grid[row][col] = label;
        }
    }
    new Timer(TIMER_DELAY, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            for (int i = 0; i < NUMBER_TO_SWAP; i++) {
                int row = random.nextInt(ROWS);
                int col = random.nextInt(COLS);
                int iconIndex = random.nextInt(ICONS.length);
                grid[row][col].setIcon(ICONS[iconIndex]);
            }
        }
    }).start();
}

From source file:Main.java

public Main() {
    label = new JLabel("Waiting...");
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(label);//  w w  w.j  a v a 2  s  . c  o  m
    frame.setSize(200, 200);
    frame.setVisible(true);

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        int count;

        @Override
        public void eventDispatched(AWTEvent event) {
            Object source = event.getSource();
            if (source instanceof Component) {
                Component comp = (Component) source;
                Window win = null;
                if (comp instanceof Window) {
                    win = (Window) comp;
                } else {
                    win = SwingUtilities.windowForComponent(comp);
                }
                if (win == frame) {
                    timer.restart();
                    label.setText("Interrupted..." + (++count));
                }
            }
        }
    }, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK
            | AWTEvent.MOUSE_WHEEL_EVENT_MASK);

    timer = new Timer(5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    });
    timer.start();
}

From source file:GUI.MyCustomFilter.java

menu(ProjectController obj, String miniFile, String outputPath) {
    projectSelected = 0;// w  ww  .  j a v  a  2  s  . c  o  m
    fileSelected = 0;
    this.miniFile = miniFile;
    this.outputPath = outputPath;
    projectControllerObject = obj;
    timer = new Timer(300, this);
    initComponents();
}

From source file:eu.delving.sip.model.ReportFileModel.java

public ReportFileModel(final SipModel sipModel) {
    this.sipModel = sipModel;
    Timer timer = new Timer(1000, new ActionListener() {
        @Override//  w  ww.j  a  va 2  s.co  m
        public void actionPerformed(ActionEvent e) {
            for (ReportFile reportFile : reportFiles) {
                List<ReportFile.Rec> fetch = reportFile.prepareFetch();
                if (!fetch.isEmpty()) {
                    sipModel.exec(reportFile.fetchRecords(fetch, sipModel.getFeedback()));
                }
                reportFile.maintainCache();
            }
        }
    });
    timer.setRepeats(true);
    timer.start();
}

From source file:Hypnosis.java

public Hypnosis(double x, double y, double r1, double r2, double a1, double a2, float linewidth, int delay,
        double deltaA, double deltaX, double deltaY) {
    this.x = x;/* ww  w.ja va 2s  .  c  om*/
    this.y = y;
    this.r1 = r1;
    this.r2 = r2;
    this.a1 = a1;
    this.a2 = a2;
    this.linewidth = linewidth;
    this.deltaA = deltaA;
    this.deltaX = deltaX;
    this.deltaY = deltaY;

    // Set up a timer to call actionPerformed() every delay milliseconds
    timer = new Timer(delay, this);

    // Create a buffer for double-buffering
    buffer = new BufferedImage((int) (2 * r2 + linewidth), (int) (2 * r2 + linewidth),
            BufferedImage.TYPE_INT_RGB);

    // Create a Graphics object for the buffer, and set the linewidth
    // and request antialiasing when drawing with it
    osg = buffer.createGraphics();
    osg.setStroke(new BasicStroke(linewidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    osg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}

From source file:Main.java

/**
 * Add component to its parent. Start the timer for auto-update.
 *///from  w ww  . j  a  v  a  2s  .  com
public void addNotify() {
    super.addNotify();
    _timer = new Timer(1000, this);
    _timer.start();
}

From source file:com.gmt2001.IniStore.java

private IniStore() {
    inifolder = LoadConfigReal("");

    t = new Timer((int) saveInterval, this);
    t2 = new Timer(1, this);

    Thread.setDefaultUncaughtExceptionHandler(com.gmt2001.UncaughtExceptionHandler.instance());

    t.start();//w w w.j  a v a 2  s  . co m
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopTimer.java

@Override
public void start() {
    if (!started) {
        timer = new Timer(delay, e -> {
            Timer timerBefore = timer;

            onTimerAction();// www . j  a v a 2s  .co m

            // if user didn't stop or restart timer
            if (timerBefore == timer && !timerBefore.isRepeats()) {
                stop();
            }
        });
        timer.setRepeats(repeating);
        timer.start();

        this.started = true;
    }
}