List of usage examples for javax.swing Timer setRepeats
public void setRepeats(boolean flag)
flag
is false
, instructs the Timer
to send only one action event to its listeners. From source file:Main.java
public static void main(String[] args) { JFrame f = new JFrame(); JTextArea textArea = new JTextArea(); f.add(new JScrollPane(textArea), BorderLayout.CENTER); Timer timer = new Timer(1000, new ActionListener() { @Override/*from w ww . ja v a 2 s . c o m*/ public void actionPerformed(ActionEvent e) { textArea.append("bla"); } }); timer.setRepeats(true); timer.start(); JButton button = new JButton("Click me"); button.addActionListener(e -> { System.out.println("Before option pane"); JOptionPane.showMessageDialog(f, "A message dialog"); System.out.println("After option pane"); }); f.add(button, BorderLayout.SOUTH); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new JLabel("Auto Hide")); frame.pack();/* www.ja v a 2 s.c o m*/ frame.setVisible(true); Timer autoHideTimer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.dispose(); } }); autoHideTimer.setRepeats(false); frame.addMouseListener(new MouseAdapter() { @Override public void mouseExited(MouseEvent e) { System.out.println("Restart..."); autoHideTimer.restart(); } @Override public void mouseEntered(MouseEvent e) { System.out.println("Stop"); autoHideTimer.stop(); } }); }
From source file:Main.java
public static void main(String[] args) { JProgressBar pb = new JProgressBar(); JTextArea ta = new JTextArea(10, 20); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new JScrollPane(ta)); frame.add(pb, BorderLayout.SOUTH); frame.pack();/*from w w w . j av a2 s . c o m*/ frame.setVisible(true); Timer timer = new Timer(250, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { index++; if (index >= 100) { ((Timer) (e.getSource())).stop(); } ta.append("Line " + index + "\n"); pb.setValue(index); } }); timer.setRepeats(true); timer.setCoalesce(true); timer.start(); }
From source file:Main.java
/** * Creates an animation to fade the dialog opacity from 0 to 1. *//*from w ww. j a v a2 s. c om*/ public static void fadeIn(final JDialog dialog) { final Timer timer = new Timer(10, null); timer.setRepeats(true); timer.addActionListener(new ActionListener() { private float opacity = 0; @Override public void actionPerformed(ActionEvent e) { opacity += 0.15f; dialog.setOpacity(Math.min(opacity, 1)); if (opacity >= 1) timer.stop(); } }); dialog.setOpacity(0); timer.start(); dialog.setVisible(true); }
From source file:Main.java
/** * Creates an animation to fade the dialog opacity from 1 to 0. *//*from w w w. j av a2s . co m*/ public static void fadeOut(final JDialog dialog) { final Timer timer = new Timer(10, null); timer.setRepeats(true); timer.addActionListener(new ActionListener() { private float opacity = 1; @Override public void actionPerformed(ActionEvent e) { opacity -= 0.15f; dialog.setOpacity(Math.max(opacity, 0)); if (opacity <= 0) { timer.stop(); dialog.dispose(); } } }); dialog.setOpacity(1); timer.start(); }
From source file:Main.java
public static void fadeIn(final Dialog win) { if (!win.isUndecorated()) { return;/*from ww w . j a v a 2 s. co m*/ } final Timer timer = new Timer(30, null); timer.setRepeats(true); timer.addActionListener(new ActionListener() { private float opacity = 0; @Override public void actionPerformed(ActionEvent e) { opacity += 0.05f; win.setOpacity(Math.min(opacity, 1f)); if (opacity >= 1) { timer.stop(); } } }); win.setOpacity(0); timer.start(); win.setVisible(true); }
From source file:Main.java
/** * Creates an animation to fade the dialog opacity from 0 to 1. *//*from w w w . j a v a2 s .co m*/ public static void fadeIn(final JDialog dialog) { final Timer timer = new Timer(10, null); timer.setRepeats(true); timer.addActionListener(new ActionListener() { private float opacity = 0; @Override public void actionPerformed(ActionEvent e) { opacity += 0.15f; dialog.setOpacity(Math.min(opacity, 1)); if (opacity >= 1) { timer.stop(); } } }); dialog.setOpacity(0); timer.start(); dialog.setVisible(true); }
From source file:Main.java
/** * Creates an animation to fade the dialog opacity from 0 to 1. * * @param dialog the dialog to fade in/*from w w w .j a va 2 s.com*/ * @param delay the delay in ms before starting and between each change * @param incrementSize the increment size */ public static void fadeIn(final JDialog dialog, int delay, final float incrementSize) { final Timer timer = new Timer(delay, null); timer.setRepeats(true); timer.addActionListener(new ActionListener() { private float opacity = 0; @Override public void actionPerformed(ActionEvent e) { opacity += incrementSize; dialog.setOpacity(Math.min(opacity, 1)); // requires java 1.7 if (opacity >= 1) { timer.stop(); } } }); dialog.setOpacity(0); // requires java 1.7 timer.start(); dialog.setVisible(true); }
From source file:Main.java
/** * Creates an animation to fade the dialog opacity from 1 to 0, and then * dispose./*from w w w . j av a 2 s . c o m*/ * * @param dialog the dialog to fade out * @param delay the delay in ms before starting and between each change * @param incrementSize the increment size */ public static void fadeOut(final JDialog dialog, int delay, final float incrementSize) { final Timer timer = new Timer(delay, null); timer.setRepeats(true); timer.addActionListener(new ActionListener() { private float opacity = 1; @Override public void actionPerformed(ActionEvent e) { opacity -= incrementSize; dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7 if (opacity < 0) { timer.stop(); dialog.dispose(); } } }); dialog.setOpacity(1); // requires java 1.7 timer.start(); }
From source file:Main.java
/** * Creates an animation to fade the dialog opacity from 0 to 1, wait at 1 * and then fade to 0 and dispose.//from w w w . j a va 2 s. c o m * * @param dialog the dialog to display * @param delay the delay in ms before starting and between each change * @param incrementSize the increment size * @param displayTime the time in ms the dialog is fully visible */ public static void fadeInAndOut(final JDialog dialog, final int delay, final float incrementSize, final int displayTime) { final Timer timer = new Timer(delay, null); timer.setRepeats(true); timer.addActionListener(new ActionListener() { private float opacity = 0; private boolean displayed = false; @Override public void actionPerformed(ActionEvent e) { if (!displayed) { opacity += incrementSize; dialog.setOpacity(Math.min(opacity, 1)); // requires java 1.7 if (opacity >= 1) { timer.setDelay(displayTime); displayed = true; } } else { timer.setDelay(delay); opacity -= incrementSize; dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7 if (opacity < 0) { timer.stop(); dialog.dispose(); } } } }); dialog.setOpacity(0); // requires java 1.7 timer.start(); dialog.setVisible(true); }