Results 1 to 2 of 2
  1. #1
    cotarelo is offline Member
    Join Date
    Mar 2010
    Rep Power

    Smile One Jspinner modifying the value of the other


    Lets say i have two spinners S1 and S2 with different values ... and when I am going to decrease the value of S2, if this value is fewer than S1, the interface does not allow me to do it.

    How to do this? Hints?

    Thank you !

    Also I would like that if I increase the value of S1, S2 is always one unit above the value of S1.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Zurich, Switzerland
    Rep Power


    Quote Originally Posted by cotarelo View Post
    Also I would like that if I increase the value of S1, S2 is always one unit above the value of S1.

    i implemented this requirement as example. when you change the first spinner, the second will change his values of sp1 + 1. changing the spinner 2 only the value of spinner 2 is changes.

    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.SpinnerNumberModel;
    public class SpinnerExample {
    	JFrame f ;
    	public SpinnerExample() {
    	f = new JFrame();
    	f.setSize(300, 400);
    	f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
    	JPanel p1 = new JPanel(); 
    	JPanel p2 = new JPanel(); 
    	f.setLayout( new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS) ); 
    	// create the two spinners with two different models
    	SpinnerNumberModel model1 = new SpinnerNumberModel( 5.0, 0.0, 10.0, 0.2 );
    	SpinnerNumberModel model3 = new SpinnerNumberModel( 5.0, 0.0, 10.0, 0.2 ); 
    	JSpinner spin1 = new JSpinner( model1 );
    	JSpinner spin3 = new JSpinner (model3);
    	spin1.addChangeListener(new SpinnerListener(spin3)); 
    	f.add(p1, BorderLayout.CENTER);
    	f.add(p2, BorderLayout.SOUTH);
    	p1.setVisible( true );
    	public static void main(String[] args) {
    		new SpinnerExample();

    here is the listener, where all changes happens

    Java Code:
    import javax.swing.JSpinner;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    public class SpinnerListener implements ChangeListener {
    	JSpinner j;
    	public SpinnerListener(JSpinner spinner) {
    		j = spinner;
    	public void stateChanged(ChangeEvent evt) {
    		JSpinner spinner = (JSpinner) evt.getSource();
    		// get the value of sp1
    		double value = Double.parseDouble(spinner.getValue().toString());
    		// set the value of sp2 according to sp1 + 1
    		j.setValue(value + 1);

    i hope, you got the idea with this example.

Similar Threads

  1. [HELP]JSpinner Left Alignment
    By Hugo in forum AWT / Swing
    Replies: 7
    Last Post: 07-25-2011, 08:02 PM
  2. JSpinner resizes, I don't want that.
    By JosAH in forum AWT / Swing
    Replies: 4
    Last Post: 05-30-2010, 09:11 AM
  3. get date, time from jSpinner
    By cowboy in forum New To Java
    Replies: 3
    Last Post: 12-08-2009, 12:32 PM
  4. Jtable Jspinner help
    By chiragkini in forum AWT / Swing
    Replies: 5
    Last Post: 08-15-2009, 04:36 AM
  5. [SOLVED] JSpinner in JTable column
    By nehaa in forum AWT / Swing
    Replies: 10
    Last Post: 05-11-2009, 01:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts