jspinner
h//:spttwww.theitroad.com
The JSpinner
is a user interface widget in Swing which allows the user to select a value from a list of predetermined values. It is similar to a combo box, but allows the user to increment or decrement the selected value using up/down arrows or the mouse scroll wheel. The JSpinner
can be used to select a range of values, such as dates or numbers.
Here is an example of how to create and use a JSpinner
in Java:
import javax.swing.*; import java.awt.*; public class JSpinnerExample { public static void main(String[] args) { JFrame frame = new JFrame("JSpinner Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create a spinner with a list of values String[] values = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; JSpinner spinner = new JSpinner(new SpinnerListModel(values)); // create a spinner with a range of values SpinnerNumberModel model = new SpinnerNumberModel(0, 0, 100, 1); JSpinner rangeSpinner = new JSpinner(model); // add the spinners to the frame JPanel panel = new JPanel(new GridLayout(2, 2)); panel.add(new JLabel("Select a day of the week: ")); panel.add(spinner); panel.add(new JLabel("Select a number between 0 and 100: ")); panel.add(rangeSpinner); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }
This code creates a frame with two spinners: one with a list of days of the week, and one with a range of numbers from 0 to 100. The JSpinner
class has several constructors which take different types of SpinnerModel
objects to define the values and behavior of the spinner.