Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to do validation using java swing?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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; January 24th, 2011 at 05:49 AM.


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: how to do validation using java swing?

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class ValidationDemo extends JFrame  implements ActionListener{
        JTextField tfName,tfCity;
        JButton btnSave;
     
        JPanel con;
     
    	public ValidationDemo(String name){
    	    super(name);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(400,400);
     
    		tfName = new JTextField(20);
    		tfCity = new JTextField(20);
    		btnSave = new JButton("Save");
    		btnSave.addActionListener(this);
     
    		con = new JPanel();
    		add(con);
    		con.add(tfName);
    		con.add(tfCity);
    		con.add(btnSave);
     
     
            setVisible(true);
    	}
     
    	public void actionPerformed(ActionEvent ae){
     
     
    		if(ae.getSource()==btnSave){
    			if(isBlank(tfName))
    				showMessage(tfName,"Please enter name");
    			else if(isBlank(tfCity))
    				showMessage(tfCity,"Please enter City");
     
                           //here you can add more else if conditions for other text fields.
     
    		}
    	}
     
    	public boolean isBlank(JTextField tf){
               //	this is to check if text field is blank
     
    			if(tf.getText().trim().equals("")){
    				return true;
    			}
    			return false;
    	}
     
    	//This function is to show error message and set focus to the box which is empty
     
    	public void showMessage(JTextField tf , String msg){
    			JOptionPane.showMessageDialog(null,msg);
    			tf.requestFocus();
    	}
     
        public static void main(String[] args) {
    		new ValidationDemo("Validation Demo");
        }
     
     
     
     
    }

    Please feel free to contact if any problem in understanding the code.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Java onLoad event after validation is complete
    By macrat101 in forum Loops & Control Statements
    Replies: 1
    Last Post: September 4th, 2010, 03:14 PM
  2. [SOLVED] Java Swing problem?
    By nasser in forum AWT / Java Swing
    Replies: 2
    Last Post: July 3rd, 2010, 12:34 PM
  3. How to Use a JSlider - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 4
    Last Post: March 29th, 2010, 09:33 AM
  4. java swing help
    By JM_4ever in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2009, 06:42 AM
  5. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM