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);
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024