• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to do validation using java swing?

 
Ranch Hand
Posts: 34
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

i have created 7 textfields and a save button. i want to validate each textfield. the scenario is if any textfield is null or left blank i should get the message box "xyz field should entered" when the save button is clicked.

example,

"class save extends JPanel {

public save() {
JButton save = new JButton("Save");
add(save);
save.addActionListener(new JDBC());
}
}"

" String exe = "insert into kics.dateframe(date,style,description,fabricgsm,agegroup,size,measurementsunits)values('" + componentTextField.getText() + "','" + TFstyle.getText() + "','" + TFdescription.getText() + "','" + TFfabricGSM.getText() + "','" + TFagegroup.getText() + "','" + TFsize.getText() + "','" + TFmeasurements.getText() + "')";

stmt.executeUpdate(exe);



i've tried this code,but its not working,
if (exe != null) {
stmt.executeUpdate(exe);
} else {
JOptionPane.showMessageDialog(null, "xyz fields should be entered");
}"


regards,
Vignesh Karthick
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vignesh,
Your first post https://coderanch.com/t/524626/GUI/java/retrieve-informations-database-publish-excel was crossposted.
You have also crossposted this question.

Please read https://coderanch.com/how-to/java/BeForthrightWhenCrossPostingToOtherSites
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please UseCodeTags, as it will make your examples much more easy to read, and as a result, much more liekly to generate helpful responses.
Please also try to make sure the code you post qualifies as a SSCCE.
Currently it's very difficult to be sure what the problem is exactly, but the variable exe will never be null, so the if-condition is useless.
Furthermore, the way you build-up your SQL statement makes your program susceptible to SQL injection. More on that here.

 
vignesh karthick
Ranch Hand
Posts: 34
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, This is the real code, database name is kics , table name is dateframe and fields are date, style, description, fabricgsm, agegroup, size, measurementsunits..

Here i want to validate componentTextField, TFstyle, TFdescription, TFfabricGSM, TFagegroup, TFsize, TFmeasurements; if these fields are null or left blank and click the save button, i must get the message box as "XYZ(date,style....) field should be entered".


public class dateFrame extends JFrame {

JTextField componentTextField = new JTextField();
JTextField TFstyle;
JTextField TFdescription;
JTextField TFfabricGSM;
JTextField TFagegroup;
JTextField TFsize;
JTextField TFmeasurements;

public static void main(String[] args) {
dateFrame df = new dateFrame();
}

public dateFrame() {
super("Date Frame");
dateFramePanel dfp = new dateFramePanel();
save s = new save();
getContentPane().add(dfp, BorderLayout.PAGE_START);
getContentPane().add(s, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(350, 250);
setResizable(false);
setVisible(true);
}

class dateFramePanel extends JPanel {

DateChooser dc = new DateChooser();

public dateFramePanel() {
super(new GridLayout(0, 2));
JLabel Ldate = new JLabel("Date");
JLabel Lstyle = new JLabel("Style");
JLabel Ldescription = new JLabel("Description");
JLabel LfabricGSM = new JLabel("Fabric & GSM");
JLabel Lagegroup = new JLabel("Age group");
JLabel Lsize = new JLabel("Size");
JLabel Lmeasurements = new JLabel("Measurement units (CM / INCH)");

TFstyle = new JTextField(10);
TFdescription = new JTextField(10);
TFfabricGSM = new JTextField(10);
TFagegroup = new JTextField(10);
TFsize = new JTextField(10);
TFmeasurements = new JTextField(10);

add(Ldate);
add(dc);
add(Lstyle);
add(TFstyle);
add(Ldescription);
add(TFdescription);
add(LfabricGSM);
add(TFfabricGSM);
add(Lagegroup);
add(TFagegroup);
add(Lsize);
add(TFsize);
add(Lmeasurements);
add(TFmeasurements);

setPreferredSize(new Dimension(700, 200));
}
}

class save extends JPanel {

public save() {
JButton save = new JButton("Save");
add(save);
save.addActionListener(new JDBC());
}
}

class JDBC implements ActionListener {

public void actionPerformed(ActionEvent e) {
Connection con = null;
Statement stmt;
String loadDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
try {
Class.forName(loadDriver);
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kics", "root", "");
stmt = con.createStatement();
String exe = "insert into kics.dateframe(date,style,description,fabricgsm,agegroup,size,measurementsunits)values('" + componentTextField.getText() + "',

'"+ TFstyle.getText() + "','" + TFdescription.getText() + "','" + TFfabricGSM.getText() + "','" + TFagegroup.getText() + "','"+ TFsize.getText() + "',

'"+ TFmeasurements.getText() + "')";

stmt.executeUpdate(exe);


} catch (Exception e1) {
System.out.println("Exception found");
System.err.println(e1.getMessage());
} finally {
try {
con.close();
} catch (Exception e1) {
System.out.println("Exception found");
System.err.print(e1.getMessage());
}
}
}
}
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jelle asked you to use code tags. There's even a link in his post. If you've missed it: UseCodeTags. You can edit your post to add them.
 
vignesh karthick
Ranch Hand
Posts: 34
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, This is the real code, database name is kics , table name is dateframe and fields are date, style, description, fabricgsm, agegroup, size, measurementsunits..

Here i want to validate componentTextField, TFstyle, TFdescription, TFfabricGSM, TFagegroup, TFsize, TFmeasurements; if these fields are null or left blank and click the save button, i must get the message box as "XYZ(date,style....) field should be entered".

Here where should i need to apply validation codes.

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The idea of posting code is to make the code readable. I doubt that you write your code "left justified", so why do you expect us to read the code "left justified". How hard is it to copy your original code into the forum and then add the code tags? You where also asked to "edit your original posting". So why did you create a new entry? Do we have to repeat ourselves twice every time we make a suggestion?
 
vignesh karthick
Ranch Hand
Posts: 34
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am very new to this javaranch. i don't know how to post the coding stuffs. i don't know the protocols to post the codes. i copied the codes from IDE and pasted here.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i copied the codes from IDE and pasted here.



You copied the code from the IDE and pasted it. When you saved the posting all the formatting was lost.

Then you where asked to use the "code" tags. So you added the code tags to the unformatted code. That highlights the code but does not add formatting.

You need to recopy the code from the IDE into your original posting and add the "Code" tags before saving the posting.
 
vignesh karthick
Ranch Hand
Posts: 34
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you want me to post in this format..

 
Ranch Hand
Posts: 92
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vignesh,

I'd say in the actionPerformed method of your action listener (which you've called JDBC), this is where you want the validation to be.

Since it is an inner class of dateFrame (btw class names should, by convension start with Upper case letter and use CamelCase e.g. DateFrame), it can see the JTextFields.

Simply check each one with the JTextComponent#getText() and compare with null or 0 length, e.g.



You'll probably want to ensure the OK button to your JOptionPane.showXXX method will not return control to that method. You only want to execute the JDBC code if all the fields are filled in afterwards.
 
vignesh karthick
Ranch Hand
Posts: 34
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi John McParland. thanks for your suggestion. i will try your method in my code. i'm beginner to java. so i dunno camelcase notations, but now onwards i follow the coding procedures. thank you.
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic