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.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »