Config the JFormattedTextField from JSpinner in Java

Description

The following code shows how to config the JFormattedTextField from JSpinner.

Example


import java.awt.BorderLayout;
import java.awt.Color;
//ww  w.  j a va2  s  .c o m
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JSpinner;


public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("JSpinner Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    
    JSpinner spinner = new JSpinner();

    // 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));

    frame.add(tf, BorderLayout.SOUTH);

    frame.setSize(200, 90);
    frame.setVisible(true);
  }
}

The code above generates the following result.

Config the JFormattedTextField from JSpinner in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer