wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Issue on putting in order the following code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Issue on putting in order the following code" Watch "Issue on putting in order the following code" New topic
Author

Issue on putting in order the following code

Marcelo Bordolli
Ranch Hand

Joined: Sep 03, 2012
Posts: 46

i have an api that have 3 textfields:

name: (gere goes textfiel1)

Last Name: (Second textfiel)

Id: (number 3 textfield)

then i have a button named SAVE , wen i hit SAVE all 3 textfields info goes to a txt.
Now the thing is that i want to control what people may put in the field i mean i dont want to save in the name textfield a number you know so i got the following code and i think i need to order this properly so this could work:

String line="";
String errorMsg = "";
boolean error = false;
int ci = 0;

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (jTextField1.getText().length() == 0 || jTextField1.getText().trim().length() == 0){
error = true;
errorMsg = "Must put a name ";

}else if (jTextField2.getText().length() == 0 || jTextField2.getText().trim().length() == 0){
error = true;
errorMsg = "Must put a last name ";

}else if (jTextField3.getText().length() == 0 || jTextField3.getText().trim().length() == 0){
error = true;
errorMsg = "Must put an ID ";

}else{
try{
ci = Integer.parseInt(jTextField3.getText());
}catch(NumberFormatException ne){
error = true;
errorMsg = "The ID must be numeric.";
}
}if(error){
JOptionPane.showMessageDialog(this, errorMsg, "Error ", JOptionPane.ERROR_MESSAGE);
}

String line= jTextField1.getText()+" "+jTextField2.getText()+" "+ jTextField3.getText()+"\n" ;

try{ //This part works fine
FileWriter fw= new FileWriter("C:\\Grupo.txt", rootPaneCheckingEnabled);
fw.write(line);
fw.close();

}catch(IOException e){};
}

Thank you ranchers !!


I know is only java
BUT I LIKE IT!!!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Please put the code inside code tags.

The code you provided is not doing what you wanted in the first place, because even if the user does not provide valid input, you are just showing a message and then you proceed with saving the input into file. What is the point of cheking if you can save invalid data?
Good practice is to do validation before performing such an action (e.g. saving to a file or database), and if the input is not valid disable the action invocation (in this case disable JButton).

Also, you're declaring line twice in your code.
Another improvement would be to implement separate methods for validating, to avoid code repetition - one method for validating text input, another for integers, etc.


The quieter you are, the more you are able to hear.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2380
    
    7

Hi please use code tags next time.

As for your question, check out DocumentListener interface.



The ValidTextChecker is an inner class




Hope this helps.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Marcelo Bordolli
Ranch Hand

Joined: Sep 03, 2012
Posts: 46

Thank you kemal i fixed the thing whit your hint i just put a jButton2.setenable(false) and another changes that i spotted and all work fine.
Thank you k man!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Any time!
I would recommend you post your code snippet after you accomplish your task with help of anyone here, so other users may benefit if they experience the same issue. There might be some further suggestions to improve your code.

Also, I noticed from your posts that you are using NetBeans GUI builder extensively, which might not be the best idea until you get comfortable with Swing.

Cheers!
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2380
    
    7

Kemal Sokolovic wrote:Also, I noticed from your posts that you are using NetBeans GUI builder extensively, which might not be the best idea until you get comfortable with Swing.


Yes I agree with Kemal about the NetBeans GUI builder. Playing with Swing components can be challenging
Marcelo Bordolli
Ranch Hand

Joined: Sep 03, 2012
Posts: 46

Thanks i will post the code to help other that may have the same issues.

And i will take your advice on the netbeans for swing. the drag and drop save you a lot of time but on the other hand it gives you new pains in the ass.
 
 
subject: Issue on putting in order the following code