How do I set Swing Timer initial delay?

The Swing javax.swing.Timer object fires the action event after the delay time specified in the constructor passed. For some reasons you might want the Timer class fires immediately after it was started.

To achieve this you can set the timer initial delay by calling the setInitialDelay(int initialDelay) method. The example below give you an example how to do it.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TimerInitialDelayDemo extends JFrame {
    private final JLabel counterLabel = new JLabel();

    private Timer timer = null;

    public TimerInitialDelayDemo() throws HeadlessException {
        initUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                () -> new TimerInitialDelayDemo().setVisible(true));
    }

    private void initUI() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(500, 500);

        Container container = getContentPane();
        container.setLayout(new FlowLayout(FlowLayout.CENTER));
        container.add(counterLabel);

        timer = new Timer(1000, new MyActionListener());

        // Set initial delay of the Timer object. Setting initial delay
        // to zero causes the timer fires the event immediately after
        // it is started.
        timer.setInitialDelay(0);
        timer.start();
    }

    class MyActionListener implements ActionListener {
        private int counter = 10;

        public void actionPerformed(ActionEvent e) {
            counter = counter - 1;
            String text = "<html><font size='14'>" +
                    counter +
                    "</font></head>";
            counterLabel.setText(text);
            if (counter == 0) {
                timer.stop();
            }
        }
    }
}

How do I use Swing Timer class?

In this example you’ll see how to use java.swing.Timer class to create a text clock program.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class TimerDemo extends JFrame {
    public static final DateFormat df = new SimpleDateFormat("hh:mm:ss");

    public TimerDemo() throws HeadlessException {
        initUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new TimerDemo().setVisible(true));
    }

    private void initUI() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(500, 500);

        Container container = getContentPane();
        container.setLayout(new FlowLayout(FlowLayout.CENTER));

        final JLabel label = new JLabel();
        container.add(label);

        Timer timer = new Timer(1000, e -> {
            Calendar now = Calendar.getInstance();
            String timeText =
                    "<html><font size='10' color='blue'>" +
                            df.format(now.getTime()) +
                            "</font></html>";
            label.setText(timeText);
        });
        timer.start();
    }
}

How do I create JSpinner component with hour value?

This example shows you how to create a JSpinner that allow you to select an hour value. As the spinner model we are using the SpinnerDateModel and set the calendar field to Calendar.HOUR_OF_DAY.

To correctly display the hour value on the spinner we also change the formatter of the spinner’s text field using SimpleDateFormatter class.

package org.kodejava.swing;

import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.DateFormatter;
import java.awt.*;
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class JSpinnerHour extends JFrame {
    public JSpinnerHour() {
        initializeUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                () -> new JSpinnerHour().setVisible(true));
    }

    private void initializeUI() {
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // The following spinner model will have current date as its
        // value and using hour of day as the calendar field. The start
        // and end comparable has a null values which mean it doesn't
        // have minimum or maximum value.
        SpinnerDateModel model = new SpinnerDateModel(new Date(), null,
                null, Calendar.HOUR_OF_DAY);

        JSpinner spinner = new JSpinner(model);

        // Reformat the display of our spinner to show only the hour
        // and minute information part.
        JFormattedTextField textField =
                ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
        DefaultFormatterFactory dff =
                (DefaultFormatterFactory) textField.getFormatterFactory();
        DateFormatter formatter = (DateFormatter) dff.getDefaultFormatter();
        formatter.setFormat(new SimpleDateFormat("hh:mm a"));

        getContentPane().add(spinner, BorderLayout.NORTH);
    }
}

How do I create JSpinner component with date value?

The SpinnerDateModel allow us to display and select date information from the JSpinner component. By default, the initial value of the model will be set to the current date. To change it we can call the setValue method of the JSpinner object.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.util.GregorianCalendar;
import java.util.Calendar;

public class JSpinnerDate extends JFrame {
    public JSpinnerDate() {
        initializeUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                () -> new JSpinnerDate().setVisible(true));
    }

    private void initializeUI() {
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // Create a SpinnerDateModel with current date as the initial value.
        SpinnerDateModel model = new SpinnerDateModel();

        // Set the spinner value to October 9, 2021.
        JSpinner spinner = new JSpinner(model);
        Calendar calendar = new GregorianCalendar(2021, Calendar.OCTOBER, 9);
        spinner.setValue(calendar.getTime());

        getContentPane().add(spinner, BorderLayout.NORTH);
    }
}

How do I create a JSpinner with a SpinnerListModel?

This example show you how to create a JSpinner component and pass a SpinnerListModel as the available values of the JSpinner component. The SpinnerListModel can hold a value of collections object and a simple array of object instance.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JSpinnerCreateDemo extends JFrame {
    public JSpinnerCreateDemo() {
        initializeUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                () -> new JSpinnerCreateDemo().setVisible(true));
    }

    private void initializeUI() {
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // Creating an array of color name that we'll use as the
        // source of our SpinnerListMode.
        String[] colors = new String[]{
                "Red", "Orange", "Yellow", "Green", "Blue", "Purple"
        };
        SpinnerListModel model = new SpinnerListModel(colors);

        // Create a JSpinner instance with a spinner model as the value.
        // This JSpinner will allow us to select a colour name when we
        // press the JButton below.
        final JSpinner spinner = new JSpinner(model);
        getContentPane().add(spinner, BorderLayout.NORTH);

        JButton okButton = new JButton("OK");
        okButton.addActionListener(e -> {
            String color = (String) spinner.getValue();
            System.out.println("Color = " + color);
        });
        getContentPane().add(okButton, BorderLayout.SOUTH);
    }
}