Example usage for javax.swing Timer isRunning

List of usage examples for javax.swing Timer isRunning

Introduction

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

Prototype

public boolean isRunning() 

Source Link

Document

Returns true if the Timer is running.

Usage

From source file:Main.java

public static void main(String[] args) {
    JProgressBar progressBar = new JProgressBar();
    JButton button = new JButton("Start");
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(progressBar);//from   www . jav a  2s  .c om
    f.add(button);

    ActionListener updateProBar = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int val = progressBar.getValue();
            if (val >= 100) {
                //  timer.stop();
                button.setText("End");
                return;
            }
            progressBar.setValue(++val);
        }
    };
    Timer timer = new Timer(50, updateProBar);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (timer.isRunning()) {
                timer.stop();
                button.setText("Start");
            } else if (button.getText() != "End") {
                timer.start();
                button.setText("Stop");
            }
        }
    });
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final Timer timer;
    final JProgressBar progressBar = new JProgressBar();
    final JButton button = new JButton("Start");
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(progressBar);//  w ww .  j  a v a  2 s  .c om
    f.add(button);

    ActionListener updateProBar = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int val = progressBar.getValue();
            if (val >= 100) {
                //  timer.stop();
                button.setText("End");
                return;
            }
            progressBar.setValue(++val);
        }
    };
    timer = new Timer(50, updateProBar);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (timer.isRunning()) {
                timer.stop();
                button.setText("Start");
            } else if (button.getText() != "End") {
                timer.start();
                button.setText("Stop");
            }
        }
    });
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:TapTapTap.java

public void createUI() {
    JFrame f = new JFrame("TapTapTap");

    final WaitLayerUI layerUI = new WaitLayerUI();
    JPanel panel = createPanel();
    JLayer<JPanel> jlayer = new JLayer<JPanel>(panel, layerUI);

    final Timer stopper = new Timer(4000, new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            layerUI.stop();/*  w w w.  j  a  va  2s  . co m*/
        }
    });
    stopper.setRepeats(false);

    mOrderButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            layerUI.start();
            if (!stopper.isRunning()) {
                stopper.start();
            }
        }
    });

    f.add(jlayer);

    f.setSize(300, 200);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:fr.duminy.jbackup.core.JBackupImplTest.java

private void waitEndOfTimerIfAny(Timer timer) throws InterruptedException {
    if (timer != null) {
        long timeout = currentTimeMillis() + 5 * timer.getDelay();
        while (timer.isRunning()) {
            if (currentTimeMillis() > timeout) {
                fail("timer not stopped");
            }/*w ww .  jav  a2  s.  c o  m*/
            Thread.sleep(100);
        }
    }
}

From source file:fr.duminy.jbackup.core.JBackupImplTest.java

private void testShutdown(boolean longTask, boolean withListener) throws Throwable {
    BackupConfiguration config = createConfiguration();
    LockableJBackup jBackup = new LockableJBackup(config.getArchiveFactory());
    if (longTask) {
        jBackup.lockCompression();//www . j av  a 2 s.c o m
    }

    TerminationListener listener = withListener ? mock(TerminationListener.class) : null;
    Future<Void> future;
    Timer timer;
    if (longTask) {
        future = jBackup.backup(config);

        timer = jBackup.shutdown(listener);
        if (withListener) {
            assertThat(timer).as("shutdown timer").isNotNull();
            assertThat(timer.isRunning()).as("timer is running").isTrue();
            verify(listener, never()).terminated();
        } else {
            assertThat(timer).as("shutdown timer").isNull();
        }
        assertThat(future.isDone()).as("isDone").isFalse();

        jBackup.unlockCompression();
        waitResult(future);
    } else {
        future = jBackup.backup(config);
        waitResult(future);
        timer = jBackup.shutdown(listener);
        if (withListener) {
            assertThat(timer).as("shutdown timer").isNotNull();
        } else {
            assertThat(timer).as("shutdown timer").isNull();
        }
    }
    waitEndOfTimerIfAny(timer);
    if (withListener) {
        verify(listener, times(1)).terminated();
        verifyNoMoreInteractions(listener);
    }
}