List of usage examples for javax.swing Timer Timer
public Timer(int delay, ActionListener listener)
From source file:EditabilityExample.java
public static void main(String[] args) { try {//ww w . j av a 2 s . c o m UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } JFrame f = new JFrame("Editability Example"); f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS)); f.getContentPane().add(firstField); JTextField tf = new JTextField("A read-only text field", 20); tf.setEditable(false); f.getContentPane().add(tf); JTextArea ta = new JTextArea("An editable\ntext area", 2, 20); ta.setBorder(BorderFactory.createLoweredBevelBorder()); f.getContentPane().add(ta); ta = new JTextArea("A read-only\ntext area", 2, 20); ta.setBorder(BorderFactory.createLoweredBevelBorder()); ta.setEditable(false); f.getContentPane().add(ta); f.pack(); f.show(); if (args.length == 1 && args[0].equals("disable")) { // Toggle the enabled state of the first // text field every 10 seconds Timer t = new Timer(10000, new ActionListener() { public void actionPerformed(ActionEvent evt) { firstField.setEnabled(!firstField.isEnabled()); firstField.setText(firstFieldText + (firstField.isEnabled() ? "" : " (disabled)")); } }); t.start(); } }
From source file:AppendingTextPane.java
public static void main(String[] args) { try {//from w w w . j ava 2s . c o m UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } JFrame f = new JFrame("Text Pane with Scrolling Append"); final AppendingTextPane atp = new AppendingTextPane(); f.getContentPane().add(new JScrollPane(atp)); f.setSize(200, 200); f.setVisible(true); // Add some text every second Timer t = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent evt) { String timeString = fmt.format(new Date()); atp.appendText(timeString + "\n"); } SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss"); }); t.start(); }
From source file:SampleProgress.java
public static void main(String args[]) { JFrame frame = new JFrame("ProgressMonitor Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new GridLayout(0, 1)); JButton startButton = new JButton("Start"); ActionListener startActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component parent = (Component) actionEvent.getSource(); monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200); progress = 0;//from w ww. j a va2 s. c om } }; startButton.addActionListener(startActionListener); frame.add(startButton); JButton increaseButton = new JButton("Manual Increase"); ActionListener increaseActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { if (monitor == null) return; if (monitor.isCanceled()) { System.out.println("Monitor canceled"); } else { progress += 5; monitor.setProgress(progress); monitor.setNote("Loaded " + progress + " files"); } } }; increaseButton.addActionListener(increaseActionListener); frame.add(increaseButton); JButton autoIncreaseButton = new JButton("Automatic Increase"); ActionListener autoIncreaseActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { if (monitor != null) { if (timer == null) { timer = new Timer(250, new ActionListener() { public void actionPerformed(ActionEvent e) { if (monitor == null) return; if (monitor.isCanceled()) { System.out.println("Monitor canceled"); timer.stop(); } else { progress += 3; monitor.setProgress(progress); monitor.setNote("Loaded " + progress + " files"); } } }); } timer.start(); } } }; autoIncreaseButton.addActionListener(autoIncreaseActionListener); frame.add(autoIncreaseButton); frame.setSize(300, 200); frame.setVisible(true); }
From source file:SystemTrayTest.java
public static void main(String[] args) { final TrayIcon trayIcon; if (!SystemTray.isSupported()) { System.err.println("System tray is not supported."); return;/*from w w w . ja v a2 s . c o m*/ } SystemTray tray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().getImage("cookie.png"); PopupMenu popup = new PopupMenu(); MenuItem exitItem = new MenuItem("Exit"); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); popup.add(exitItem); trayIcon = new TrayIcon(image, "Your Fortune", popup); trayIcon.setImageAutoSize(true); trayIcon.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { trayIcon.displayMessage("How do I turn this off?", "Right-click on the fortune cookie and select Exit.", TrayIcon.MessageType.INFO); } }); try { tray.add(trayIcon); } catch (AWTException e) { System.err.println("TrayIcon could not be added."); return; } final List<String> fortunes = readFortunes(); Timer timer = new Timer(10000, new ActionListener() { public void actionPerformed(ActionEvent e) { int index = (int) (fortunes.size() * Math.random()); trayIcon.displayMessage("Your Fortune", fortunes.get(index), TrayIcon.MessageType.INFO); } }); timer.start(); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(0, 1)); // Define Start Button JButton startButton = new JButton("Start"); ActionListener startActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component parent = (Component) actionEvent.getSource(); monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200); progress = 0;//from www .ja v a2 s .c o m } }; startButton.addActionListener(startActionListener); contentPane.add(startButton); // Define Manual Increase Button // Pressing this button increases progress by 5 JButton increaseButton = new JButton("Manual Increase"); ActionListener increaseActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { if (monitor == null) return; if (monitor.isCanceled()) { System.out.println("Monitor canceled"); } else { progress += 5; monitor.setProgress(progress); monitor.setNote("Loaded " + progress + " files"); } } }; increaseButton.addActionListener(increaseActionListener); contentPane.add(increaseButton); // Define Automatic Increase Button // Start Timer to increase progress by 3 every 250 ms JButton autoIncreaseButton = new JButton("Automatic Increase"); ActionListener autoIncreaseActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { if (monitor != null) { if (timer == null) { timer = new Timer(250, new ProgressMonitorHandler()); } timer.start(); } } }; autoIncreaseButton.addActionListener(autoIncreaseActionListener); contentPane.add(autoIncreaseButton); frame.setSize(300, 200); frame.setVisible(true); }
From source file:SampleProgress.java
public static void main(String args[]) { JFrame frame = new JFrame("ProgressMonitor Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(0, 1)); // Define Start Button JButton startButton = new JButton("Start"); ActionListener startActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component parent = (Component) actionEvent.getSource(); monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200); progress = 0;//from ww w. java2 s.co m } }; startButton.addActionListener(startActionListener); contentPane.add(startButton); // Define Manual Increase Button // Pressing this button increases progress by 5 JButton increaseButton = new JButton("Manual Increase"); ActionListener increaseActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { if (monitor == null) return; if (monitor.isCanceled()) { System.out.println("Monitor canceled"); } else { progress += 5; monitor.setProgress(progress); monitor.setNote("Loaded " + progress + " files"); } } }; increaseButton.addActionListener(increaseActionListener); contentPane.add(increaseButton); // Define Automatic Increase Button // Start Timer to increase progress by 3 every 250 ms JButton autoIncreaseButton = new JButton("Automatic Increase"); ActionListener autoIncreaseActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { if (monitor != null) { if (timer == null) { timer = new Timer(250, new ProgressMonitorHandler()); } timer.start(); } } }; autoIncreaseButton.addActionListener(autoIncreaseActionListener); contentPane.add(autoIncreaseButton); frame.setSize(300, 200); frame.setVisible(true); }
From source file:SwingTimerDemo.java
public static void main(String[] args) { // Run a default fixed-delay timer timer = new Timer(DELAY, new SwingTimerDemo()); startTime = prevTime = System.currentTimeMillis(); System.out.println("Fixed Delay Times"); timer.start();//www .j a v a 2s .c o m // Sleep for long enough that the first timer ends try { Thread.sleep(DURATION * 2); } catch (Exception e) { } // Run a timer with no coalescing to get fixed-rate behavior timer = new Timer(DELAY, new SwingTimerDemo()); startTime = prevTime = System.currentTimeMillis(); timer.setCoalesce(false); System.out.println("\nFixed Rate Times"); timer.start(); }
From source file:Main.java
public static void runTimer(int duration, final Runnable run) { Timer t = new Timer(duration, new ActionListener() { public void actionPerformed(ActionEvent e) { run.run();//from w w w . j a v a2s . c om } }); t.setRepeats(false); t.start(); }
From source file:Main.java
/** * Creates an animation to fade the dialog opacity from 0 to 1. *///from w w w . java 2s . 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 a 2 s . 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(); }