Create a spinner : Spinner « Swing JFC « Java






Create a spinner

Create a spinner
   
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 orientation = SwingConstants.VERTICAL;

  private BasicArrowButton incrementButton;

  private BasicArrowButton decrementButton;

  public Spinner() {
    createComponents();
  }

  public Spinner(int o) {
    orientation = o;
    createComponents();
  }

  public void setEnabled(boolean enable) {
    incrementButton.setEnabled(enable);
    decrementButton.setEnabled(enable);
  }

  public boolean isEnabled() {
    return (incrementButton.isEnabled() && decrementButton.isEnabled());
  }

  protected void createComponents() {
    if (orientation == SwingConstants.VERTICAL) {
      setLayout(new GridLayout(2, 1));
      incrementButton = new BasicArrowButton(SwingConstants.NORTH);
      decrementButton = new BasicArrowButton(SwingConstants.SOUTH);
      add(incrementButton);
      add(decrementButton);
    } else if (orientation == SwingConstants.HORIZONTAL) {
      setLayout(new GridLayout(1, 2));
      incrementButton = new BasicArrowButton(SwingConstants.EAST);
      decrementButton = new BasicArrowButton(SwingConstants.WEST);
      add(decrementButton);
      add(incrementButton);
    }
  }

  public JButton getIncrementButton() {
    return (incrementButton);
  }

  public JButton getDecrementButton() {
    return (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);
    frame.pack();
    frame.show();
  }
}


           
         
    
    
  








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.Demonstrating the JSpinner ComponentDemonstrating the JSpinner Component
6.Demonstrate the Swing Spinner control
7.Spinner 2Spinner 2
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