Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default how to do validation using java swing?

    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".

    Java 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()); 
    } 
    } 
    } 
    }
    Last edited by Vignesh Karthick; 01-24-2011 at 10:42 AM. Reason: to show full code.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    Hi,
    What is problem? What can't you do it?
    You need before submit form checked fields, if there is empty show alert window.
    for example Validate textfield in Java Swing
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    kire9dk is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default Re: how to do validation using java swing?

    Hi,

    I just to ask from which lib is this DateChooser class?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Help!! Validation for java
    By peppy99 in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2011, 12:55 PM
  2. Help!! Validation for java
    By peppy99 in forum New To Java
    Replies: 14
    Last Post: 01-07-2011, 10:33 AM
  3. Swing Validation
    By ppayal in forum AWT / Swing
    Replies: 0
    Last Post: 02-09-2008, 09:00 AM
  4. Swing validation
    By ppayal in forum New To Java
    Replies: 0
    Last Post: 02-09-2008, 08:59 AM
  5. java validation?
    By lockmac in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •