-2

So I need to know how to create and implement a JSpinner. I scoured the web for three hours to little avail. All I learned was that there aren't any decent tutorials for an inexperienced programmer on how to create OR implement a spinner. So far what I've done with my program is the same as creating a new window, except that the JSpinner absolutely refuses to display in the window I made. If anyone has any tips for making it display, that would be wonderful. Here's my code:

            JFrame window = new JFrame("Determining Character Stats");
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            textLabel.setPreferredSize(new Dimension(300, 100));
            window.getContentPane().add(textLabel, BorderLayout.CENTER);
            window.setSize(1000, 500);
            window.setLocationRelativeTo(null);
            window.setVisible(true);

            JSpinner test = new JSpinner();
            test.setEnabled(true);
            test.setSize(200,200);
            test.setVisible(true);
6
  • 2
    You clearly didn't look very hard. Searching for "java tutorial jspinner" returns many different JSpinner tutorials. Sep 19, 2015 at 20:58
  • 3
    "All I learned was that there aren't any decent tutorials for an inexperienced programmer on how to create OR implement a spinner." -- I agree with @DavidPostill. The Oracle JSpinner Tutorial is the canonical one, the one that myself and camickr learned how to use this tool, and likely the 1st hit of any Google search. If it confuses you, then let us know about your specific confusion, but don't blame the tutorial as it is most definitely more than adequate. Sep 19, 2015 at 21:06
  • I checked those sites. They're all too in-depth. But thank you for the help.
    – minxed
    Sep 20, 2015 at 0:12
  • 2
    @minxed: you're looking at the tutorial in much too concrete a fashion. No, you'll not find any tutorial that shows you how to create a JSpinner exactly how you'll need to use it, but the tutorial that camickr and I've linked to will show you the general principles of use of a JSpinner, including how to use spinner models, how to respond to changes in teh spinner's state, generally everything you need to know to understand the class. Then you take the general information and try to apply it to your specific situation. This is how all programming tutorials work, and the key skill you'll.. Sep 20, 2015 at 0:25
  • 1
    ... need is just that: the ability to apply any general information that you learn to your specific situation. In fact, I think that this is one of the key skills that any and every programming student needs to master, because if a student is successful at this, they will find learning to program to be a joy, but if the opposite is true, and they don't learn this skill, then programming just isn't for that student. Not everyone can learn this stuff, but fortunately most can. Luck. Sep 20, 2015 at 0:27

2 Answers 2

3

All I learned was that there aren't any decent tutorials

Start with the Swing tutorial on How to Use Spinners for working examples. You might want to read the whole tutorial.

By the way did you read the JSpinner API? The API also has a link to the tutorial.

except that the JSpinner absolutely refuses to display in the window

If you want a component to display, then you actually need to add it to the frame (BEFORE you make the frame visible).

You also don't need to set the size or make spinner visible.

2
  • 1
    Also see the tutorial about layout managers, you will need it sooner or later docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
    – Cinnam
    Sep 19, 2015 at 21:01
  • The ironic thing is that (s)he knows how to add components, as the window.getContentPane().add(textLabel, BorderLayout.CENTER); line proves.
    – Robin
    Sep 20, 2015 at 9:08
0

First off all, to display a JSpinner, this must be first created. As far I can see, the parent window, and the spinner are created. The control cannot display, if it wasn't added to another window. How do I know this? Maybe from working with other applications. I have a general idea, of how the windows applications can work.

Many controls are made to be painted by their container objects. Just think what would be happen, if the control is painted first. And after that, if the parent window would be painted afterwards. The parent window can cover the control and this won't be visible.

Even though the control is visible, this is not enough to display. In your case, the JSpinner control can be added to a parent window:

window.add(test); // This is all you need, to display the control 

I forgott to say, that I used a javax.swing.JDialog, not a JFrame. Maybe I had some reasons for not using JFrame, I don't remember well. An abstract interface, can't be used directly, it would have to be implemented. And the JDialog interface seemed to be the perfect implementation, that I need.

Doing just a simple search on Google, you can find few more details. JFrame is a class, that inherits directly from a base abstract class Frame. Since you're writting this class, that means you make this implementation. But the most difficult part of this implementation, it is already made by the parent class JFrame.

I created a more complex dialog interface with many other controls. That JSpinner control it has been added to a parent JPanel control. It could have been added directly to the main application window. But in a real application, the arrangement of the controls could be more complex.

I also didn't know, how to set the JSpinner to work in my Dialog. I need a spinner control, to display an area for a housing project. But the defaut behaviour of the spinner, was to display integers. And I want to display a floating point number having two digits.

At the class level, you can declare a double variable, that will keep the value of the spinner control:

private static double val; // this variable will keep the value 

In my JDialog class I have a function, that creates all the controls. This is how I created this control:

java.awt.Font font = 
    new java.awt.Font("Arial", java.awt.Font.BOLD, 16); 

DoubleSpinnerModel model = new DoubleSpinnerModel(); 
model.setValue(0.00);       // The initial value is 0.00
model.setMinimum(0.00);         // It doesn't accept negative values 
model.setMaximum(10000.00);     // Can be enough for an area in sqm
model.setStepSize(0.01);        // Little change step of 0.01

javax.swing.JSpinner editAREA = new javax.swing.JSpinner(model); 
editAREA.setFont(font); 
editAREA.addChangeListener(new javax.swing.event.ChangeListener() {  
    @Override
    public void stateChanged(javax.swing.event.ChangeEvent e) { 
        // Area value changes in floating number with 2 digits 
        val = (double)((javax.swing.JSpinner)e.getSource()).getValue(); 
        // Do whatever with this value, but not here...
    } 
}); 
gr11.add(editAREA); 

Don't call any methods of the spinner in the body of stateChanged. Because errors can be thrown and the value will not longer change. Just use the sender event source object, from that argument event e. This is a thread safe function, so that no error should be thrown. Here you could get just a numeric value.

gr11 is the parent JPanel group, about that I was talking before. To use this control, it must be first created and added to window. I had created a BOLD font, because the text was not so visible. And the default style of the control doesn't look quite so well. I just can say, that I could not change too much this appearance.

Now it follows the most interesting part. You would wonder, what is that model, that i had declared first. Usually it must be created a javax.swing.SpinnerNumberModel object. But in this case, I want a different format for displaying numbers.

Into another file, I had created a custom class for this goal. It's about a DoubleSpinnerModel class, inherited from SpinnerNumberModel. The java file must have the same name, as the new created class. Next, you will find the implementation for this custom class:

public class DoubleSpinnerModel extends javax.swing.SpinnerNumberModel {  
  
  private static double f = 0.00; 
  private static double min = 0.00; 
  private static double max = 10000.00; 
  private static double step = 0.01; 

  public static void main(String args[]) { 
    
  } 

  @Override
  public Object getValue() { 
    f = (double)Double.parseDouble(super.getValue().toString()); 
    return f; 
  } 

  @Override
  public void setValue(Object value) { 
    f = (double)value; 
    super.setValue(f); 
    this.fireStateChanged(); 
  } 

  @Override
  public Object getNextValue() {
    f = (double)Double.parseDouble(super.getValue().toString()); 
    f = f + step; 
    max = (double)this.getMaximum(); 
    if (f > max) f = max; 
    return f; 
  } 

  @Override
  public Object getPreviousValue() { 
    f = (double)Double.parseDouble(super.getValue().toString()); 
    f = f - step; 
    min = (double)this.getMinimum(); 
    if (f < min) f = min; 
    return f; 
  } 

  public void setStepSize(Object stepSize){
    step = (double)stepSize; 
  }
} 

By default, all those values was set before to certain integer numbers. But this class uses double numbers, instead of using integer values. A main function must be declared, to call a base class constructor.

If you use just the private static values, stored in the class definition, you must take care, that all these values are available to all instances of this class. For a particular control, you must get the superclass values, as follows:

f = (double)Double.parseDouble(super.getValue().toString()); 

Please note, that this NumberModel, cannot be used for more controls. If you add this model to more spinners, all controls will also display the same value. Changing a value of a spinner, all the other spinners will change with the same values. From this reason, you must create another new model for each other spinner control.

Another useful improvement can be applied to the number format itself. When pasting a double number over this control, the new displayed value could not fit into this number model, it shows more digits, depending the existing number. So, the entered value could be formatted to a fixed double model of 2 digits. The setValue function can be changed like this:

@Override
public void setValue(Object value) { 
   f = (double) Double.parseDouble(String.format("%.2f", value)); 
   super.setValue(f); 
   this.fireStateChanged(); 
} 

The NumberModel is the equivalent of an Up-Down control, which could be attached to an edit control. ( Like in Visual Basic ) And this control is subclassed by the parent control. If you create a JSpinner control from a NumberModel, you actually subclass the NumberModel to the container JSpinner. Therefore the JSpinner is a superclass for the NumberModel.

In principle, this is what you can do with this JSpinner control. And I hope, that now it is more clearer what is working, and how. This is what I had tried myself, and it's working like this.

With kind regards, Adrian Brinas.

Not the answer you're looking for? Browse other questions tagged or ask your own question.