I'm using Netbeans IDE I want to validate a text field that use to type the discount percentage. so that I want to stop typing numbers more than 100 in keytyped event.
Can anyone help me to solve this problem
I'm using Netbeans IDE I want to validate a text field that use to type the discount percentage. so that I want to stop typing numbers more than 100 in keytyped event.
Can anyone help me to solve this problem
Don't use KeyListener
s to try and filter text components.
Instead...
DocumentFilter
. This will allow you to filter the content of the field BEFORE it's applied to the document. Check out Implementing a DocumentFilter and examples for more infoJSpinner
. Check out How to use SpinnersUpdated with example
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestSpinner02 {
public static void main(String[] args) {
new TestSpinner02();
}
public TestSpinner02() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JSpinner spinner = new JSpinner();
spinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(spinner);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}