Wednesday, December 31, 2008


Disable Keyboard Editing in JSpinner

A simple example on JSpinner. In the example, the editing mode is disabled using setEditable() method. So it will not be possible to insert numbers with the help of keyboard :


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

public class Disablekeyboard{
public static void main(String[] args){
JFrame frm = new JFrame("Disable Keyboard Editing Example");
JSpinner spinner = new JSpinner();
JFormattedTextField tf = ((JSpinner.DefaultEditor)spinner.getEditor())
.getTextField();
tf.setEditable(false);
spinner.setValue(new Integer(100));
JPanel panel = new JPanel();
panel.add(spinner);
frm.add(panel, BorderLayout.NORTH);
frm.setSize(350, 350);
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


0 comments: