Java Swing How to - Format Editor for JSpinner








Question

We would like to know how to format Editor for JSpinner.

Answer

/* ww  w . ja va 2 s . com*/
import java.awt.FlowLayout;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;

public class Main extends JFrame {

  public Main () {
    setSize(200, 100);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));

    add(new JLabel("Expiration Date:"));
    Date today = new Date();
    JSpinner s = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH));
    JSpinner.DateEditor de = new JSpinner.DateEditor(s, "MM/yy");
    s.setEditor(de);
    add(s);

    setVisible(true);
  }

  public static void main(String args[]) {
    new Main();
  }
}