Java Swing How to - Create String List based spinner








Question

We would like to know how to create String List based spinner.

Answer

/* www.  j  a v  a 2  s . c  o  m*/
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;

public class Main extends JFrame {
  public Main() {
    JSpinner m_listSpinner;
    SpinnerListModel m_listSpinnerModel;
    List<String> sequence = new ArrayList<String>();

    sequence.add(new String("I - First"));
    sequence.add(new String("II - Second"));
    sequence.add(new String("III - Third"));
    sequence.add(new String("IV - Fourth"));
    sequence.add(new String("V - Fifth"));
    sequence.add(new String("VI - Sixth"));

    m_listSpinnerModel = new SpinnerListModel(sequence);

    m_listSpinner = new JSpinner(m_listSpinnerModel);
    add(m_listSpinner);
  }

  public static void main(String argv[]) {
    Main spinnerFrame = new Main();
    spinnerFrame.setSize(350, 200);
    spinnerFrame.setVisible(true);
  }
}