Stack Overflow Asked by random on February 25, 2021
I implement OOP in my Java assignment. But when I started creating user interface and accept the input I have faced a problem. I wanted to validate the user input of jTextField from user interface using my setter method. I want a pop up to appear when user input is invalid instead of just error message. I know it can be done easily if I implement the validation code directly in the user interface. I don’t know which way is better but since I already have all my setter method so I wanted to validate using setter method.
public void setUsername(String username){
if(username.equals(null)){
//validation method
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Admin ad = new Admin();
String username = jTextField6.getText();
ad.setUsername(username);
}
Validation must be done in several layers to ensure your application accuracy .But here according to your situation you are trying to validate an user input.So it's a validation which belongs to view layer like you are validating forms in web development.So since you are using Java Swing you need to place your view validation logic inside the controller.Because of that the best way to implement it is like below.
String username = jTextField6.getText();
if(userName.isEmpty()){
//Place your error message logic here
}else{
ad.setUsername(username);
}
Answered by Hasindu Dahanayake on February 25, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP