- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a List Spinner in Java?
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 −
Advertisements