473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to do validation using java swing?

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,

Expand|Select|Wrap|Line Numbers
  1. "class save extends JPanel {
  2.  
  3.         public save() {
  4.             JButton save = new JButton("Save");
  5.             add(save);
  6.             save.addActionListener(new JDBC());
  7.         }
  8.     }"
  9.  
  10. " 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() + "')";
  11.  
  12.  stmt.executeUpdate(exe);
  13.  

i've tried this code,but its not working,
Expand|Select|Wrap|Line Numbers
  1.                 if (exe != null) {
  2.                     stmt.executeUpdate(exe);
  3.                 } else {
  4.                     JOptionPane.showMessageDialog(null, "xyz fields should be entered");
  5.                 }"

regards,
Vignesh Karthick
Jan 24 '11 #1

✓ answered by horace1

if I have JTextField and a JButton and when I click the button I check that there is text in textfiled the JButton actionPerformed() method looks like
Expand|Select|Wrap|Line Numbers
  1.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  2.         if(jTextField1.getText().length()==0)
  3.             JOptionPane.showMessageDialog(null, "enter text in textfield");
  4.         else
  5.             JOptionPane.showMessageDialog(null, "textfield is " + jTextField1.getText());
  6.     }
  7.  

4 21932
horace1
1,510 Expert 1GB
the getText() method in JTextField returns a String so you could check if it is empty so (assuming exe is a String set up using getText())
Expand|Select|Wrap|Line Numbers
  1. if (exe.equals("")) {  
  2.   // string empty
  3.  
or
Expand|Select|Wrap|Line Numbers
  1. if (exe.length() == 0) {  
  2.   // string empty
  3.  
Jan 24 '11 #2
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.

Expand|Select|Wrap|Line Numbers
  1. public class dateFrame extends JFrame { 
  2.  
  3. JTextField componentTextField = new JTextField(); 
  4. JTextField TFstyle; 
  5. JTextField TFdescription; 
  6. JTextField TFfabricGSM; 
  7. JTextField TFagegroup; 
  8. JTextField TFsize; 
  9. JTextField TFmeasurements; 
  10.  
  11. public static void main(String[] args) { 
  12. dateFrame df = new dateFrame(); 
  13.  
  14. public dateFrame() { 
  15. super("Date Frame"); 
  16. dateFramePanel dfp = new dateFramePanel(); 
  17. save s = new save(); 
  18. getContentPane().add(dfp, BorderLayout.PAGE_START); 
  19. getContentPane().add(s, BorderLayout.SOUTH); 
  20. pack(); 
  21. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  22. setLocation(350, 250); 
  23. setResizable(false); 
  24. setVisible(true); 
  25.  
  26. class dateFramePanel extends JPanel { 
  27.  
  28. DateChooser dc = new DateChooser(); 
  29.  
  30. public dateFramePanel() { 
  31. super(new GridLayout(0, 2)); 
  32. JLabel Ldate = new JLabel("Date"); 
  33. JLabel Lstyle = new JLabel("Style"); 
  34. JLabel Ldescription = new JLabel("Description"); 
  35. JLabel LfabricGSM = new JLabel("Fabric & GSM"); 
  36. JLabel Lagegroup = new JLabel("Age group"); 
  37. JLabel Lsize = new JLabel("Size"); 
  38. JLabel Lmeasurements = new JLabel("Measurement units (CM / INCH)"); 
  39.  
  40. TFstyle = new JTextField(10); 
  41. TFdescription = new JTextField(10); 
  42. TFfabricGSM = new JTextField(10); 
  43. TFagegroup = new JTextField(10); 
  44. TFsize = new JTextField(10); 
  45. TFmeasurements = new JTextField(10); 
  46.  
  47. add(Ldate); 
  48. add(dc); 
  49. add(Lstyle); 
  50. add(TFstyle); 
  51. add(Ldescription); 
  52. add(TFdescription); 
  53. add(LfabricGSM); 
  54. add(TFfabricGSM); 
  55. add(Lagegroup); 
  56. add(TFagegroup); 
  57. add(Lsize); 
  58. add(TFsize); 
  59. add(Lmeasurements); 
  60. add(TFmeasurements); 
  61.  
  62. setPreferredSize(new Dimension(700, 200)); 
  63.  
  64. class save extends JPanel { 
  65.  
  66. public save() { 
  67. JButton save = new JButton("Save"); 
  68. add(save); 
  69. save.addActionListener(new JDBC()); 
  70.  
  71. class JDBC implements ActionListener { 
  72.  
  73. public void actionPerformed(ActionEvent e) { 
  74. Connection con = null; 
  75. Statement stmt; 
  76. String loadDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
  77. try { 
  78. Class.forName(loadDriver); 
  79. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kics", "root", ""); 
  80. stmt = con.createStatement(); 
  81. String exe = "insert into kics.dateframe(date,style,description,fabricgsm,agegroup,size,measurementsunits)values('" + componentTextField.getText() + "', 
  82.  
  83. '"+ TFstyle.getText() + "','" + TFdescription.getText() + "','" + TFfabricGSM.getText() + "','" + TFagegroup.getText() + "','"+ TFsize.getText() + "', 
  84.  
  85. '"+ TFmeasurements.getText() + "')"; 
  86.  
  87. stmt.executeUpdate(exe); 
  88.  
  89.  
  90. } catch (Exception e1) { 
  91. System.out.println("Exception found"); 
  92. System.err.println(e1.getMessage()); 
  93. } finally { 
  94. try { 
  95. con.close(); 
  96. } catch (Exception e1) { 
  97. System.out.println("Exception found"); 
  98. System.err.print(e1.getMessage()); 
  99. }
Jan 24 '11 #3
horace1
1,510 Expert 1GB
if I have JTextField and a JButton and when I click the button I check that there is text in textfiled the JButton actionPerformed() method looks like
Expand|Select|Wrap|Line Numbers
  1.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  2.         if(jTextField1.getText().length()==0)
  3.             JOptionPane.showMessageDialog(null, "enter text in textfield");
  4.         else
  5.             JOptionPane.showMessageDialog(null, "textfield is " + jTextField1.getText());
  6.     }
  7.  
Jan 24 '11 #4
Thank you horace1.. your idea is working. now i can implement this into my code...
Jan 24 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Scott Khan | last post by:
We have a Java Swing position. Need consultants ASAP. Position : Java Swing XML / SOAP - FX / Brokerage Requirements : Swing - JTrees / Jtables. other swing classes. etc. Java/ J2EE. JDK...
1
by: iamvani | last post by:
hi , i am creating a sign in page but it is not completed .can u help me create a signin page using java(swing).or jsp
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
10
by: yeshello54 | last post by:
Hey guys i am pretty new to java swing and need some help. I am developing a simple color chooser program in swing. I have a color panel that is connected to three sliders. red green and blue. but...
1
by: Shankarsawant | last post by:
Hi, I want to run crystal repot file in java swing, can anybody help me.
2
by: Selva123 | last post by:
Hi All, Greetings. I want to test JAVA SWING application with PERL, do we have any module to do so (like win32::GUI for windows)?. or some third party free tools integrated with PERL? I am...
3
by: Akino877 | last post by:
Hello, I have a Java Swing application that I would like to be able to forward to or to run it - sorry, I am not sure if I am using the right term - from a JSP page. And I would like for my Java...
1
by: JanineXD | last post by:
Hey, I seem to have a problem on a Java Swing Program with ActionListener. I've tried to use getSource in our class but seem don't have an idea why it won't work when I tried to program at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.