0

I wish to know if there's a way to set a minimum value and a maximum value which should be both integers to my Jtextfield input

What I succceeded to do is restricting the values entered to numeric only which is this

private void tfBMKeyTyped(java.awt.event.KeyEvent evt) {                              
    // TODO add your handling code here:
    char vchar = evt.getKeyChar();
    if(!(Character.isDigit(vchar))
        || (vchar == KeyEvent.VK_BACK_SPACE)
        || (vchar == KeyEvent.VK_DELETE)){
    evt.consume();
}   
} 

I just want to know if I can apply a range of integers to be entered to this. For examples (integers >= 1 and Integers <= 20)

Labelle Doriane
  • 105
  • 1
  • 3
  • 15
  • 4
    1. Use the right tool: Don't use a JTextField but rather a JSpinner, one whose model is restricted to 1-20. 2. You should never use a KeyListener on a JTextField as that can mess with the functionality of the field. Use a higher level construct such as a DocumentFilter or validator if needed. – Hovercraft Full Of Eels Nov 01 '18 at 22:39
  • 2
    Or you might want to look at `JFormattedTextField` [tutorial](https://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html) [javadoc](https://docs.oracle.com/javase/8/docs/api/javax/swing/JFormattedTextField.html). You shouldn't have to be working at the level of individual keystrokes to achieve your goal here. – Kevin Anderson Nov 01 '18 at 22:42
  • I am trying to implement the document filter and I just have one quick question. how can I apply the document filter to the input when getting the values. for example int n4 = Integer.parseInt(tfN4.getText()); @HovercraftFullOfEels – Labelle Doriane Nov 01 '18 at 23:24
  • You would never use code like that. There are 3 methods that you would override within a DocumentFilter, and you'd get the text from the parameters to those methods. [For example](https://stackoverflow.com/a/11093360/522444) – Hovercraft Full Of Eels Nov 01 '18 at 23:26
  • @HovercraftFullOfEels I have created two classes documentFilter and MyIntFilter in my code but what I still don't get is this how to get the text from the parameters to those methods. I am still a beginner in Java so i'm a little bit confused. Can you please elaborate more on this? – Labelle Doriane Nov 02 '18 at 00:26
  • Did you look at my answer that I linked to in my comment above? If not, please do so now, since in that answer I show how to extract a relevant String from the method parameters. – Hovercraft Full Of Eels Nov 02 '18 at 00:56
  • 1
    ......................? – Hovercraft Full Of Eels Nov 03 '18 at 02:36
  • yeah I saw that but when I tried it it didn't go well unfortunately for me – Labelle Doriane Nov 03 '18 at 08:46

1 Answers1

0

In fact you can enter any value into a JTextField (there is no restrictions), but you can creat a loop to restrict someone from entering invalid numbers. For example:

if (number>=1 && number <=20){
    String message = "You number is in range";
        JOptionPane.showMessageDialog(null, message, "Output", JOptionPane.PLAIN_MESSAGE);
    }
else {String message = "You number is out of range";
    JOptionPane.showMessageDialog(null, message, "Output", JOptionPane.PLAIN_MESSAGE);
    System.exit(0);}
    }
Sergei Voychuk
  • 171
  • 2
  • 8
  • yup it seems working but Its gives the message and allows the code to run afterwards even when the number is out of range – Labelle Doriane Nov 01 '18 at 23:38
  • 2
    @LabelleDoriane, `allows the code to run afterwards even when the number is out of range` - which is why you use a JSpinner or DocumentFilter as was suggested in the first comment. – camickr Nov 01 '18 at 23:56
  • Usually it's Ok if the code is running. If you don't neet it simply add close operation for your JFrame or JPanel to the "else" stage of the loop. – Sergei Voychuk Nov 02 '18 at 01:39
  • You can use System.exit(0) in the end of the "else" loop to stop the programm or initiate any other part of the code. It's depends on what do you really need. – Sergei Voychuk Nov 02 '18 at 01:51