Spinner 2 : Spinner « Swing JFC « Java

Spinner 2

Spinner 2
Swing, Second Edition
by Matthew Robinson, Pavel Vorobiev


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicArrowButton;

public class Spinner extends JPanel {
  private int m_orientation = SwingConstants.VERTICAL;

  private BasicArrowButton m_incrementButton;

  private BasicArrowButton m_decrementButton;

  public Spinner() {

  public Spinner(int orientation) {
    m_orientation = orientation;

  public void setEnabled(boolean enable) {

  public boolean isEnabled() {
    return (m_incrementButton.isEnabled() && m_decrementButton.isEnabled());

  protected void createComponents() {
    if (m_orientation == SwingConstants.VERTICAL) {
      setLayout(new GridLayout(2, 1));
      m_incrementButton = new BasicArrowButton(SwingConstants.NORTH);
      m_decrementButton = new BasicArrowButton(SwingConstants.SOUTH);
    } else if (m_orientation == SwingConstants.HORIZONTAL) {
      setLayout(new GridLayout(1, 2));
      m_incrementButton = new BasicArrowButton(SwingConstants.EAST);
      m_decrementButton = new BasicArrowButton(SwingConstants.WEST);

  public JButton getIncrementButton() {
    return (m_incrementButton);

  public JButton getDecrementButton() {
    return (m_decrementButton);

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = (JPanel) frame.getContentPane();
    panel.setLayout(new BorderLayout());
    JTextField field = new JTextField(20);
    Spinner spinner = new Spinner();

    panel.add(field, "Center");
    panel.add(spinner, "East");

    Dimension dim = frame.getToolkit().getScreenSize();
    frame.setLocation(dim.width / 2 - frame.getWidth() / 2, dim.height / 2
        - frame.getHeight() / 2);

Related examples in the same category

1.A program to test spinners.A program to test spinners.
2.An implementation of JSpinner with customized content--iconsAn implementation of JSpinner with customized content--icons
3.A quick test of various spinnersA quick test of various spinners
4.An example of JSpinner with a custom editorAn example of JSpinner with a custom editor
5.Create a spinnerCreate a spinner
6.Demonstrating the JSpinner ComponentDemonstrating the JSpinner Component
7.Demonstrate the Swing Spinner control
8.Listening for Changes to the Value in a JSpinner Component
9.Creating a SpinnerListModel That Loops Through Its Values
10.Customizing the Editor in a JSpinner Component: Create a color spinner
11.Setting the Margin Space on a JSpinner Component
12.Limiting the Values in a Number JSpinner Component
13.Disabling Keyboard Editing in a JSpinner Component
14.Creating an Hour JSpinner Component
15.Creating a JSpinner Component: A number spinner:
16.A list spinner
17.A date spinner
18.A spinner of dates
19.Use an Icon Editor for use with the JSpinner component
20.Use images in tooltips
21.Number spinner
22.Date spinner
23.List based spinner
24.Java Spinner ComponentJava Spinner Component
25.Enhanced Spinner List Formatter
26.Enhanced Spinner List Model