How to do validation using java swing?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VigneshKarthick
    New Member
    • Jan 2011
    • 5

    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,

    Code:
    "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,
    Code:
                    if (exe != null) {
                        stmt.executeUpdate(exe);
                    } else {
                        JOptionPane.showMessageDialog(null, "xyz fields should be entered");
                    }"

    regards,
    Vignesh Karthick
    Last edited by Niheel; Jan 24 '11, 03:00 PM.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    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())
    Code:
    if (exe.equals("")) {  
      // string empty
    or
    Code:
    if (exe.length() == 0) {  
      // string empty

    Comment

    • VigneshKarthick
      New Member
      • Jan 2011
      • 5

      #3
      Hi, This is the real code, database name is kics , table name is dateframe and fields are date, style, description, fabricgsm, agegroup, size, measurementsuni ts..

      Here i want to validate componentTextFi eld, 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.

      Code:
      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()); 
      } 
      } 
      } 
      }

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        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
        Code:
            private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
                if(jTextField1.getText().length()==0)
                    JOptionPane.showMessageDialog(null, "enter text in textfield");
                else
                    JOptionPane.showMessageDialog(null, "textfield is " + jTextField1.getText());
            }

        Comment

        • VigneshKarthick
          New Member
          • Jan 2011
          • 5

          #5
          Thank you horace1.. your idea is working. now i can implement this into my code...

          Comment

          Working...