SWT Spinner Sample : Spinner « SWT JFace Eclipse « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » SWT JFace Eclipse » SpinnerScreenshots 
SWT Spinner Sample
SWT Spinner Sample

/*
 
 * (c) Copyright IBM Corp. 2000, 2001.
 
 * All Rights Reserved.
 *  
 */

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TypedListener;

public class Spinner extends Composite {

  static final int BUTTON_WIDTH = 16;

  Text text;

  Button up, down;

  int minimum, maximum;

  public Spinner(Composite parent, int style) {

    super(parent, style);

    text = new Text(this, style | SWT.SINGLE | SWT.BORDER);

    up = new Button(this, style | SWT.ARROW | SWT.UP);

    down = new Button(this, style | SWT.ARROW | SWT.DOWN);

    text.addListener(SWT.Verify, new Listener() {

      public void handleEvent(Event e) {

        verify(e);

      }

    });

    text.addListener(SWT.Traverse, new Listener() {

      public void handleEvent(Event e) {

        traverse(e);

      }

    });

    up.addListener(SWT.Selection, new Listener() {

      public void handleEvent(Event e) {

        up();

      }

    });

    down.addListener(SWT.Selection, new Listener() {

      public void handleEvent(Event e) {

        down();

      }

    });

    addListener(SWT.Resize, new Listener() {

      public void handleEvent(Event e) {

        resize();

      }

    });

    addListener(SWT.FocusIn, new Listener() {

      public void handleEvent(Event e) {

        focusIn();

      }

    });

    text.setFont(getFont());

    minimum = 0;

    maximum = 9;

    setSelection(minimum);

  }

  void verify(Event e) {

    try {

      Integer.parseInt(e.text);

    catch (NumberFormatException ex) {

      e.doit = false;

    }

  }

  void traverse(Event e) {

    switch (e.detail) {

    case SWT.TRAVERSE_ARROW_PREVIOUS:

      if (e.keyCode == SWT.ARROW_UP) {

        e.doit = true;

        e.detail = SWT.NULL;

        up();

      }

      break;

    case SWT.TRAVERSE_ARROW_NEXT:

      if (e.keyCode == SWT.ARROW_DOWN) {

        e.doit = true;

        e.detail = SWT.NULL;

        down();

      }

      break;

    }

  }

  void up() {

    setSelection(getSelection() 1);

    notifyListeners(SWT.Selection, new Event());

  }

  void down() {

    setSelection(getSelection() 1);

    notifyListeners(SWT.Selection, new Event());

  }

  void focusIn() {

    text.setFocus();

  }

  public void setFont(Font font) {

    super.setFont(font);

    text.setFont(font);

  }

  public void setSelection(int selection) {

    if (selection < minimum) {

      selection = minimum;

    else if (selection > maximum) {

      selection = maximum;

    }

    text.setText(String.valueOf(selection));

    text.selectAll();

    text.setFocus();

  }

  public int getSelection() {

    return Integer.parseInt(text.getText());

  }

  public void setMaximum(int maximum) {

    checkWidget();

    this.maximum = maximum;

    resize();

  }

  public int getMaximum() {

    return maximum;

  }

  public void setMinimum(int minimum) {

    this.minimum = minimum;

  }

  public int getMinimum() {

    return minimum;

  }

  void resize() {

    Point pt = computeSize(SWT.DEFAULT, SWT.DEFAULT);

    int textWidth = pt.x - BUTTON_WIDTH;

    int buttonHeight = pt.y / 2;

    text.setBounds(00, textWidth, pt.y);

    up.setBounds(textWidth, 0, BUTTON_WIDTH, buttonHeight);

    down.setBounds(textWidth, pt.y - buttonHeight, BUTTON_WIDTH,
        buttonHeight);

  }

  public Point computeSize(int wHint, int hHint, boolean changed) {

    GC gc = new GC(text);

    Point textExtent = gc.textExtent(String.valueOf(maximum));

    gc.dispose();

    Point pt = text.computeSize(textExtent.x, textExtent.y);

    int width = pt.x + BUTTON_WIDTH;

    int height = pt.y;

    if (wHint != SWT.DEFAULT)
      width = wHint;

    if (hHint != SWT.DEFAULT)
      height = hHint;

    return new Point(width, height);

  }

  public void addSelectionListener(SelectionListener listener) {

    if (listener == null)
      throw new SWTError(SWT.ERROR_NULL_ARGUMENT);

    addListener(SWT.Selection, new TypedListener(listener));

  }

}




           
       
Related examples in the same category
1.SWT Spinner TestSWT Spinner Test
2.Floating point values in SWT SpinnerFloating point values in SWT Spinner
3.create and initialize a SWT spinner widgetcreate and initialize a SWT spinner widget
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.