Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:37:01+00:00 2026-06-02T20:37:01+00:00

I made a JSpinner component with a SpinnerNumberModel as model like this: JSpinner spinbox

  • 0

I made a JSpinner component with a SpinnerNumberModel as model like this:

JSpinner spinbox = new JSpinner(new SpinnerNumberModel(2, 1, 7, 1));

I added a listener to detect changes.

spinbox.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent arg0) {
        synchronized (this) {
            System.out.println("New value: " +
              ((Integer)_spiNumOfAIs.getValue()).intValue());
        }
    }
});

All I see, however, is this:

New value: 0

New value: 0

Am I reading out the value of the spinbox incorrectly?

In case you’re wondering about the synchronized block: I’m new to Swing and I don’t know whether action listeners are executed on the same thread where I created my JDialog or not, so I put in thread safety measures for all action listeners to be safe. _spiNumOfAIs is a member variable.

Edit: even from this small code snippet it’s so obvious that I didn’t even see it… while refactoring I forgot that I’m using both a JSpinner spinbox and a JSpinner _spiNumOfAis, with the former being the spinner that is visible in the GUI.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:37:02+00:00Added an answer on June 2, 2026 at 8:37 pm

    All Swing operations should be performed on the EDT, and as a result interacting with your UI always happens on the EDT. In short, your ChangeListener will be called on the EDT so no need for the synchronize. You can easily verify this by using EventQueue.isDispatchThread in your listener, which will return true. This is explained in the Swing concurrency lesson, from which I quote:

    The event dispatch thread, where all event-handling code is executed. Most code that interacts with the Swing framework must also execute on this thread.

    To further answer your problem with the incorrect value, you should post more code allowing us to reproduce your problem. For example this is a very basic working example of a spinner

    import javax.swing.JFrame;
    import javax.swing.JSpinner;
    import javax.swing.SpinnerModel;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.awt.EventQueue;
    
    public class JSpinnerDemo {
    
      public static JFrame createFrame(){
        JFrame frame = new JFrame(  );
        frame.add( createSpinner() );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.pack();
    
        return frame;
      }
    
      public static JSpinner createSpinner(){
        JSpinner spinner = new JSpinner( new SpinnerNumberModel( 2,1,7,1 ) );
        spinner.addChangeListener( new ChangeListener() {
          @Override
          public void stateChanged( ChangeEvent e ) {
            JSpinner spinner = ( JSpinner ) e.getSource();
            SpinnerModel spinnerModel = spinner.getModel();
            System.out.println(spinnerModel.getValue());
          }
        } );
        return spinner;
      }
      public static void main( String[] args ) {
        EventQueue.invokeLater( new Runnable() {
          @Override
          public void run() {
            createFrame().setVisible( true );
          }
        } );
      }
    }
    

    Feel free to modify this to illustrate your problem. I can also suggest to take a look at the JSpinner class javadoc since the editor and the model value might get out of sync, as explained in the javadoc. Another good starting point is the Swing tutorial about spinners

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Made a new project, added main.cpp and wrote the code at this URL: http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp
Made some changes to my Django app's model and used South to migrate them
I made a class for DB connection like this: public class DbHelper : IDisposable
Made this nice little loop for hiding and showing div's, works as a charm
Made a fiddle for this: http://jsfiddle.net/terjeto/MN4FJ/ My problem is that dragleave fires when you
Made a new window application. Right-clicked Frameworks > Add Existing Frameworks. Selected Frameworks folder,
Made a custom ListCellRenderer: import java.awt.Component; import javax.swing.JCheckBox; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel;
Made a simple server using TcpListen and would like to add SSL to the
I made a custom DatePicker that extends the android DatePicker UI component. I needed
I made some changes in my master branch and want to bring those upstream.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.