How to create a List Spinner in Java?

Java 8Object Oriented ProgrammingProgramming

For a List Spinner, use the SpinnerListModel class. At first, let us set a list −

SpinnerListModel list = new SpinnerListModel(new String[] { "Football","Cricket", "Hockey", "Squash", "Fencing" });

Now, set it and create a new JSpinner −

JSpinner spinner = new JSpinner(list);

The following is an example to create a List Spinner −

Example

package my;
import java.awt.GridBagLayout;
import javax.swing.*;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame("Spinner Demo");
      JPanel panel = new JPanel();
      JLabel label = new JLabel("Favourite Sports − ");
      panel.setLayout(new GridBagLayout());
      SpinnerListModel list = new SpinnerListModel(new String[] { "Football","Cricket", "Hockey", "Squash", "Fencing" });
      JSpinner spinner = new JSpinner(list);
      spinner.setBounds(50, 80, 70, 100);
      panel.add(label);
      panel.add(spinner);
      frame.add(panel);
      frame.setSize(600, 300);
      frame.setVisible(true);
   }
}

This will produce the following output −


raja
Published on 03-Jun-2019 11:16:10
Advertisements