Reputation: 1
I have made form in swing with 6 jTextFields. User should be able to input only numbers from 1 to 48 in each of Text fields. There should be also check to see if any of numbers repeat and app should check if any field is left blank.
I know how to do this in VB.net but i'm new to java in general.
In VB.net i would put all textfields in array and then check to see if all text fields are filled in, are the numbers in range and check if there is a duplicate number in any field. If all criteria is met i would proceed to send inputs to database.
I am fairly new to java so if anyone could help me or point me in right direction that would be awesome.
Upvotes: 0
Views: 1051
Reputation: 347184
There a number of ways that this might be achieved depending on how you want to achieve it...
Use a JFormattedTextField
, this will validate the user inpur when the field is actioned or focus is lost, discard, what it considers to be, invalid input.
This is good for post validation of most common formats but does lack some flexibility...
Use a JSpinner
, this will validate the user input after the field is actioned or focus is lost. This is good when you want to allow the user to quickly move between values without the need to actually type something (click up or down is much easier the selecting the field, moving to the right, deleting the last character and incrementing the value by one...for example :P)
Use a InputVerifier
to perform your own validation and take appropriate steps based on your needs.
Technically, you could use this with the other solutions, but it might effect how JFormattedField
and JSpinner
work, so be careful.
This provides you with the ability to define the actual rules used to post validate the field when it is actioned or focus is lost. It also allows you to decide how focus transversal should work when the field is invalid, allowing to maintain focus on the field until the value is valid if you want.
You could also display an error message here as well...
Use a DocumentFilter
, which would allow you to do real time valildation of the input as the user makes it, discard invalid characters in real time.
This is one of the most complex solutions, but can also be one of the most useful.
For examples see MDP's blog (archived).
Upvotes: 5
Reputation: 3650
Here's an example of what the validation could look like:
JTextField[] fields;
void vaildate() {//call when appropriate(ie ActionListener of JButton)
int[] nums = new int[fields.length];
Set<Integer> set = new HashSet<>();
for (int i = 0; i < fields.length; i++) {
try {
nums[i] = Integer.parseInt(fields[i].getText());
} catch (NumberFormatException ex) {
//not a valid number tell user of error
return;
}
if (nums[i] < 1 || nums[i] > 48) {
//out of number range tell user of error
return;
}
if (!set.add(nums[i])) {
//duplicate element tell user of error
return;
}
}
//do what you want with the data now
}
Upvotes: 0
Reputation: 509
JTextField field = new JTextField();
do{
int number = Interger.parseInterger(field.getText()); // this is how you will parse it into int
if (number < 1 || number is > 48){
System.Out.Println("Error, try again");
}
}while(number > 0 || number is < 49)
Upvotes: 0