PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   26-07-2021

Clock Timer

! Swing timer fires events after a specific delay. You might use the general purpose timer (from java.util) if you don't use GUI.
 
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.TimeZone;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

class Appexample extends JFrame {
    
    private JTextField textField;
    
    public Appexample() {
        
        textField = new JTextField(5);
        textField.setEditable(false);
        textField.setFont(new Font("sansserif", Font.PLAIN, 48));
        textField.setBorder(new EmptyBorder(0,0,0,0));
        
        JPanel panel = new JPanel();
        panel.add(textField);
        
        add(panel);
        
        // Look Here - one second
        javax.swing.Timer timer = 
            new javax.swing.Timer(1000, new ClockListener());
        timer.start();
    }

    public static void main(String[] args) {
        JFrame App = new Appexample();
        App.setBounds(200, 200, 300, 200);
        App.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        App.setVisible(true);
    }
    
    class ClockListener implements ActionListener { // --- Look Here --- //
    
        @Override public void actionPerformed(ActionEvent e) {
            
            Calendar now = Calendar.getInstance();
            now.setTimeZone(TimeZone.getTimeZone("Europe/Bucharest"));
            
            int h = now.get(Calendar.HOUR_OF_DAY);
            int m = now.get(Calendar.MINUTE);
            int s = now.get(Calendar.SECOND);
            
            textField.setText(h + ":" + m + ":" + s);
        }
    }
}
... 35 lines
 
Write text char by char
 
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

class App extends JFrame {
    
    private JTextField textField;
    private int i = 0;
    private String str = "I idly turn the leaves";
    private javax.swing.Timer timer;
    
    public App() {
        
        textField = new JTextField(12);
        textField.setEditable(false);
        textField.setFont(new Font("sansserif", Font.PLAIN, 30));
        textField.setBorder(new EmptyBorder(0,0,0,0));

        JPanel panel = new JPanel();
        panel.add(textField);
        add(panel);
        
        /**
         * Use full qualification to avoid 
         * potential conflicts with java.util.Timer
         */
        timer = new javax.swing.Timer(100, new ClockListener()); // Look Here
        timer.start();
    }

    public static void main(String[] args) {
        JFrame App = new App();
        App.setBounds(200, 200, 400, 300);
        App.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        App.setVisible(true);
    }
    
    class ClockListener implements ActionListener { // Look Here
    
        @Override public void actionPerformed(ActionEvent e) {
            
            if (++i >= str.length()) timer.stop();

            textField.setText(str.substring(0, i));
        }
    }
}
... 31 lines
 

Questions    
Designer

        A B C D E F
🔔
1/1