Question

My jSpinner does not reflect the value to JLabel when I change the value using keyboard.

When I use up down arrows works fine !

enter image description here

When I use keyboard JLabel remains with previous value

enter image description here

Here is the code below

private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {                                       

    value = (Integer)jSpinner1.getValue();
    jLabel1.setText(value.toString());
 }                                      

  private void jSpinner1KeyTyped(java.awt.event.KeyEvent evt) {                                   

   value = (Integer)jSpinner1.getValue();
   jLabel1.setText(value.toString());

}    
Was it helpful?

Solution 4

I did in this way , I right click on jSpinner in design view and customize the code add this code below now it works fine , jSpinner1 is only allowing the numbers so no any other character.

 jSpinner1 = new javax.swing.JSpinner();
 jSpinner1.setFont(new java.awt.Font("Calibri", 0, 14)); 
 jSpinner1.setModel(new javax.swing.SpinnerNumberModel(1, 1, 1000, 1));
 JFormattedTextField field1 = ((JSpinner.NumberEditor)        
 jSpinner1.getEditor()).getTextField();
 ((NumberFormatter) field1.getFormatter()).setAllowsInvalid(false);
 DefaultFormatter formatter1 = (DefaultFormatter) field1.getFormatter();
 formatter1.setCommitsOnValidEdit(true);

And this the state change event

    private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) 
    {                                       
           value = (Integer)jSpinner1.getValue();
           jLabel1.setText(value.toString());
    } 

OTHER TIPS

My jSpinner does not reflect the value to JLabel when I change the value using keyboard.

  • don't to use KeyListener for Swing JComponents, KeyListener doesn't react correctly to BackSpace, nor for inserted array of chars from ClipBoard (copy -> paste)

  • use DocumentListener added to derived editor JTextField/JFormattedTextField from JSpinner,

First, don't use KeyListeners, they are just horrible little creatures...

Second, JSpinner itself is not the editor, but a container for other components, as such, you need to ask the JSpinner for the editor, for example, from the JavaDocs

JComponent editor = spinner.getEditor()
if (editor instanceof DefaultEditor) {
    //...
}

Now, DefaultEditor is still a compound class, you need to ask for the JTextField that is acutally been used by the editor...

JFormattedTextField field = ((DefaultEditor)editor).getTextField();

From here, you can simply attach a DocumentListener to the field.

The user needs to hit Enter to make the new value be noticed by the model/listener etc.. Either that or the or keys. If the value is outside the allowable range or otherwise invalid, no action occurs. See below for the source used to confirm that in the default Metal PLAF.

Forget the KeyListener, it is a dead end here.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

class TimeBeforeClass {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new FlowLayout(FlowLayout.LEFT, 3,3));
                gui.add(new JLabel("Show"));
                final JLabel output = new JLabel("Output..");
                final JSpinner sp = new JSpinner(
                        new SpinnerNumberModel(15,0,20,1));
                sp.addChangeListener(new ChangeListener() {

                    @Override
                    public void stateChanged(ChangeEvent e) {
                        output.setText(sp.getValue() + " minutes.");
                    }
                });
                gui.add(sp);
                gui.add(new JLabel("minutes before class"));

                JPanel mainUI = new JPanel(new BorderLayout(5, 5));
                mainUI.add(output, BorderLayout.PAGE_END);
                mainUI.add(gui);

                JOptionPane.showMessageDialog(null, mainUI);
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top