Java JSpinner disable editor field

Description

Java JSpinner disable editor field

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class Main extends JFrame {
   public Main() {
      super("java2s.com");

      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setLayout(new FlowLayout());

      SpinnerNumberModel  m_numberSpinnerModel = new SpinnerNumberModel();
      JSpinner spinner = new JSpinner(m_numberSpinnerModel);
      //w  ww.j  ava2 s . c  o m
      // Disable keyboard edits in the spinner
      JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
      tf.setEditable(false);
      tf.setBackground(Color.white);

      // The value of the spinner can still be programmatically changed
      spinner.setValue(new Integer(100));
      
      getContentPane().add(spinner);
   }

   public static void main(String[] args) {
      Main frame = new Main();
      frame.pack();
      frame.setVisible(true);
   }
}



PreviousNext

Related