Java Swing How to - Create String value based JSpinner








Question

We would like to know how to create String value based JSpinner.

Answer

import java.awt.BorderLayout;
import java.text.DateFormatSymbols;
import java.util.Locale;
/* ww w .  j  a  v a  2  s .c  o  m*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH);

    String days[] = symbols.getWeekdays();
    SpinnerModel model1 = new SpinnerListModel(days);
    JSpinner spinner1 = new JSpinner(model1);

    JLabel label1 = new JLabel("French Days/List");
    JPanel panel1 = new JPanel(new BorderLayout());
    panel1.add(label1, BorderLayout.WEST);
    panel1.add(spinner1, BorderLayout.CENTER);
    frame.add(panel1, BorderLayout.NORTH);

    frame.setSize(200, 90);
    frame.setVisible(true);
  }
}