Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2006

Tip: Looking for answers? Try searching our database.

JSpinner question

Thread view: 
Alex - 17 Jul 2006 18:08 GMT
I want my JSpinner control to change step based on current value of
spinner

Like this: if spinner value is less that 10 step is 1, if spinner value
becomes more than 10 step will be 3

Thanks,
Alex
Roland de Ruiter - 17 Jul 2006 21:57 GMT
> I want my JSpinner control to change step based on current value of
> spinner
[quoted text clipped - 4 lines]
> Thanks,
> Alex

Create your own implementation of the SpinnerModel interface and set
this as the model of your JSpinner. For example:

import java.awt.BorderLayout;
import javax.swing.AbstractSpinnerModel;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SwingUtilities;

public class MySpinnerModel extends AbstractSpinnerModel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MySpinnerModel mySpinnerModel = new MySpinnerModel();
                JSpinner spinner = new JSpinner();
                spinner.setModel(mySpinnerModel);
                JFrame app = new JFrame();
                app.setSize(400, 300);
                app.getContentPane().add(spinner, BorderLayout.NORTH);
                app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                app.validate();
                app.setVisible(true);
            }
        });
    }

    private int lowerBound = -20;

    private int upperBound = +20;

    private int value = 0;

    public Object getNextValue() {
        int newValue;
        if (this.value <= 10) {
            newValue = this.value + 1;
        } else {
            newValue = this.value + 3;
        }
        if (newValue <= upperBound) {
            return new Integer(newValue);
        } else {
            return null;
        }
    }

    public Object getPreviousValue() {
        int newValue;
        if (this.value <= 10) {
            newValue = this.value - 1;
        } else {
            newValue = this.value - 3;
        }
        if (newValue >= lowerBound) {
            return new Integer(newValue);
        } else {
            return null;
        }
    }

    public Object getValue() {
        return new Integer(value);
    }

    private void setValue(int value) {
        if (this.value != value) {
            this.value = value;
            fireStateChanged();
        }
    }

    public void setValue(Object value) {
        if (value instanceof String) {
            try {
                setValue(Integer.parseInt(((String) value).trim()));
            } catch (NumberFormatException ex) {
                System.err.println("Unsupported value: " + value);
            }
        } else if (value instanceof Number) {
            setValue(((Number) value).intValue());
        } else {
            System.err.println("Unsupported value: " + value);
        }
    }
}

Signature

Regards,

Roland



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.