January 07, 2012

Positive Number chooser in java


How to create textfield in java that accept only positive integer value

This example shows a small demonstration of java text field that accepts only numbers. The project is developed in java swing.

import javax.swing.*;
/**
*
* @author James
*
*/
class PositiveNumberChooser {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SpinnerNumberModel numberModel = new SpinnerNumberModel(
new Integer(15), // value
new Integer(10), // min
new Integer(30), // max
new Integer(1) // step
);
JSpinner numberChooser = new JSpinner(numberModel);
JOptionPane.showMessageDialog(null, numberChooser);
System.out.println("Number: " + numberChooser.getValue());
}
});
}
}

Through this post i am introducing a blog for java developers name "belazy".  Need your feedback and comments. so please try the code ....

Facebook comments