validating text fields
hi i am using NETBEANS IDE so when i want to validate TEXTFEILDS if one textfield is blank then it should focus on that particular textfield so help me with this plz.....
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SAdd.java
*
* Created on Jan 1, 2006, 1:34:23 AM
*/
package menu;
import java.sql.*;
/**
*
* @author Vortex
*/
public class SAdd extends javax.swing.JFrame {
/** Creates new form SAdd */
public SAdd() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
TRollNo = new javax.swing.JTextField();
TName = new javax.swing.JTextField();
TAge = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
TAdd = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
CCourse = new javax.swing.JComboBox();
jLabel7 = new javax.swing.JLabel();
CClass = new javax.swing.JComboBox();
jButton2 = new javax.swing.JButton();
BAdd = new javax.swing.JButton();
Clear = new javax.swing.JButton();
LHostel = new javax.swing.JLabel();
Hostel = new javax.swing.JComboBox();
jLabel5 = new javax.swing.JLabel();
RMale = new javax.swing.JRadioButton();
RFemale = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel1.setText("Roll No");
TRollNo.setFont(new java.awt.Font("Tahoma", 1, 12));
TRollNo.setAutoscrolls(false);
TName.setFont(new java.awt.Font("Tahoma", 1, 12));
TName.setAutoscrolls(false);
TName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TNameActionPerformed(evt);
}
});
TAge.setFont(new java.awt.Font("Tahoma", 1, 12));
TAge.setAutoscrolls(false);
TAdd.setColumns(20);
TAdd.setFont(new java.awt.Font("Tahoma", 1, 12));
TAdd.setRows(5);
jScrollPane1.setViewportView(TAdd);
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel2.setText("Name");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel3.setText("Address");
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel4.setText("Age");
jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel6.setText("Course");
CCourse.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
CCourse.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "BCA", "BBM", "B.Com", "BSC", "BA" }));
CCourse.setSelectedIndex(-1);
CCourse.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CCourseActionPerformed(evt);
}
});
jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel7.setText("Class");
CClass.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
CClass.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "I", "II", "III" }));
CClass.setSelectedIndex(-1);
CClass.setPreferredSize(new java.awt.Dimension(65, 21));
CClass.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CClassActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton2.setText("Back");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
BAdd.setFont(new java.awt.Font("Tahoma", 1, 12));
BAdd.setText("ADD");
BAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BAddActionPerformed(evt);
}
});
Clear.setFont(new java.awt.Font("Tahoma", 1, 12));
Clear.setText("Clear");
Clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearActionPerformed(evt);
}
});
LHostel.setFont(new java.awt.Font("Tahoma", 1, 12));
LHostel.setText("Hostel Name");
Hostel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
Hostel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hostel 1", "Hostel 2", "Hostel 3", "Hostel 4" }));
Hostel.setSelectedIndex(-1);
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel5.setText("Sex");
buttonGroup1.add(RMale);
RMale.setFont(new java.awt.Font("Tahoma", 1, 12));
RMale.setText("Male");
buttonGroup1.add(RFemale);
RFemale.setFont(new java.awt.Font("Tahoma", 1, 12));
RFemale.setText("Female");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel5)
.addComponent(jLabel6)
.addComponent(LHostel, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGap(53, 53, 53)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(13, 13, 13)
.addComponent(RMale)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, Short.MAX_VALUE)
.addComponent(RFemale, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
.addComponent(TName, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
.addComponent(TRollNo, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
.addComponent(TAge, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(Hostel, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(CCourse, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(29, 29, 29)
.addComponent(jLabel7)
.addGap(18, 18, 18)
.addComponent(CClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(568, 568, 568))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(534, Short.MAX_VALUE)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Clear)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(BAdd)
.addGap(278, 278, 278))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(TRollNo, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TName, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(TAge, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(RMale)
.addComponent(RFemale))
.addGap(45, 45, 45)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(CCourse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(CClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7))
.addGap(59, 59, 59)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(LHostel)
.addComponent(Hostel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(49, 49, 49)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(BAdd)
.addComponent(Clear)
.addComponent(jButton2))
.addGap(23, 23, 23))
);
RMale.getAccessibleContext().setAccessibleName("");
RFemale.getAccessibleContext().setAccessibleName("");
pack();
}// </editor-fold>//GEN-END:initComponents
private void TNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TNameActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_TNameActionPerformed
private void CCourseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CCourseActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_CCourseActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
menu.Menu mn = new menu.Menu();
mn.setVisible(true);
this.dispose();
this.setVisible(false);
}//GEN-LAST:event_jButton2ActionPerformed
private void CClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CClassActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_CClassActionPerformed
private void BAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BAddActionPerformed
// TODO add your handling code here:
int flag,i,j,k=0;
if(TRollNo.getText().isEmpty())
{
javax.swing.JOptionPane.showMessageDialog(null,"Enter the Roll No","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
TRollNo.requestFocusInWindow();
flag=1;
}
else
{
flag=0;
}
if(TName.getText().isEmpty())
{
javax.swing.JOptionPane.showMessageDialog(null,"Enter the Name","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
flag=1;
}
else
{
flag=0;
}
if(TAdd.getText().isEmpty())
{
javax.swing.JOptionPane.showMessageDialog(null,"Enter the Address","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
flag=1;
}
else
{
flag=0;
}
if(TAge.getText().isEmpty())
{
javax.swing.JOptionPane.showMessageDialog(null,"Enter the Age","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
flag=1;
}
else
{
flag=0;
}
if(CCourse.getSelectedItem()==null)
{
javax.swing.JOptionPane.showMessageDialog(null,"Select Course","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
flag=1;
}
else
{
flag=0;
}
if(CClass.getSelectedItem()==null)
{
javax.swing.JOptionPane.showMessageDialog(null,"Select Class","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
flag=1;
}
else
{
flag=0;
}
if((RMale.isSelected()==false) && (RFemale.isSelected()==false))
{
javax.swing.JOptionPane.showMessageDialog(null,"Select Sex","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
flag=1;
}
else
{
flag=0;
}
if( Hostel.getSelectedItem()==null)
{
javax.swing.JOptionPane.showMessageDialog(null,"Select Hostel","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
flag=1;
}
else
{
flag=0;
}
if(flag==0)
{
String c=CCourse.getSelectedItem().toString();
String cl=CClass.getSelectedItem().toString();
String Hos=Hostel.getSelectedItem().toString();
int Roll=Integer.parseInt(TRollNo.getText());
String Name=TName.getText();
String Add=TAdd.getText();
int Age=Integer.parseInt(TAge.getText());
boolean male=RMale.isSelected();
boolean female=RFemale.isSelected();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Stu","admin","admin");
PreparedStatement stm;
PreparedStatement stm1;
PreparedStatement stm2;
stm=con.prepareStatement("insert into Student values(?,?,?,?,?,?,?)");
stm.setInt(1,Roll);
stm.setString(2,Name);
stm.setString(3,Add);
stm.setInt(4,Age);
stm.setString(5,c);
stm.setString(6,cl);
if(male==true)
{
stm.setString(7,"Male");
}
if(female==true)
{
stm.setString(7,"Female");
}
i=stm.executeUpdate();
stm1=con.prepareStatement("insert into Fees (Roll,Hostel) values(?,?)");
stm1.setInt(1,Roll);
stm1.setString(2,Hos);
j=stm1.executeUpdate();
stm2=con.prepareStatement("insert into Fees1 (ROll) values(?)");
stm2.setInt(1,Roll);
k=stm2.executeUpdate();
if(i>0&j>0&k>0)
{
javax.swing.JOptionPane.showMessageDialog(null,"Sucessfully Updated","Updation",javax.swing.JOptionPane.INFORMATION_MESSAGE);
TRollNo.setText("");
TName.setText("");
TAdd.setText("");
TAge.setText("");
CCourse.setSelectedIndex(-1);
CClass.setSelectedIndex(-1);
Hostel.setSelectedIndex(-1);
buttonGroup1.clearSelection();
}
else
{
javax.swing.JOptionPane.showMessageDialog(null,"Item not available","Error",javax.swing.JOptionPane.ERROR_MESSAGE);
}
con.close();
}
catch(Exception e){
javax.swing.JOptionPane.showMessageDialog(null,"Cannot update","Error",javax.swing.JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_BAddActionPerformed
}
private void ClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearActionPerformed
// TODO add your handling code here:
TRollNo.setText("");
TName.setText("");
TAdd.setText("");
TAge.setText("");
CCourse.setSelectedIndex(-1);
CClass.setSelectedIndex(-1);
Hostel.setSelectedIndex(-1);
buttonGroup1.clearSelection();
}//GEN-LAST:event_ClearActionPerformed
public class Myaction implements java.awt.event.ActionListener {
public void actionPerformed(java.awt.event.ActionEvent evte) {
TName.setText(evte.getActionCommand());
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SAdd().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton BAdd;
private javax.swing.JComboBox CClass;
private javax.swing.JComboBox CCourse;
private javax.swing.JButton Clear;
private javax.swing.JComboBox Hostel;
private javax.swing.JLabel LHostel;
private javax.swing.JRadioButton RFemale;
private javax.swing.JRadioButton RMale;
public javax.swing.JTextArea TAdd;
public javax.swing.JTextField TAge;
public javax.swing.JTextField TName;
public javax.swing.JTextField TRollNo;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
public javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}
View Answers
March 15, 2010 at 12:57 PM
Hi Friend,
We have modified your BAddActionPerformed() method. Try the following:
private void BAddActionPerformed(java.awt.event.ActionEvent evt) {
if(TRollNo.getText().length()==0){
JOptionPane.showMessageDialog(null,"Enter the Roll No","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
}
else if(TName.getText().length()==0){
JOptionPane.showMessageDialog(null,"Enter the Name ","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
}
else if(TAdd.getText().length()==0){
JOptionPane.showMessageDialog(null,"Enter the Addresss ","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
}
else if(TAge.getText().length()==0){
JOptionPane.showMessageDialog(null,"Enter the Age ","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
}
else if((RMale.isSelected()==false) && (RFemale.isSelected()==false))
{
JOptionPane.showMessageDialog(null,"Select Sex","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
}
else if(CCourse.getSelectedItem()==null){
JOptionPane.showMessageDialog(null,"Select Course","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
}
else if(CClass.getSelectedItem()==null){
JOptionPane.showMessageDialog(null,"Select Class","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
}
else if( Hostel.getSelectedItem()==null)
{
JOptionPane.showMessageDialog(null,"Select Hostel","Empty",javax.swing.JOptionPane.WARNING_MESSAGE);
}
else{
String c=CCourse.getSelectedItem().toString();
String cl=CClass.getSelectedItem().toString();
String Hos=Hostel.getSelectedItem().toString();
int Roll=Integer.parseInt(TRollNo.getText());
String Name=TName.getText();
String Add=TAdd.getText();
int Age=Integer.parseInt(TAge.getText());
boolean male=RMale.isSelected();
boolean female=RFemale.isSelected();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Stu","admin","admin");
PreparedStatement stm;
PreparedStatement stm1;
PreparedStatement stm2;
stm=con.prepareStatement("insert into Student values(?,?,?,?,?,?,?)");
stm.setInt(1,Roll);
stm.setString(2,Name);
stm.setString(3,Add);
stm.setInt(4,Age);
stm.setString(5,c);
stm.setString(6,cl);
if(male==true)
{
stm.setString(7,"Male");
}
if(female==true)
{
stm.setString(7,"Female");
}
i=stm.executeUpdate();
stm1=con.prepareStatement("insert into Fees (Roll,Hostel) values(?,?)");
stm1.setInt(1,Roll);
stm1.setString(2,Hos);
j=stm1.executeUpdate();
stm2=con.prepareStatement("insert into Fees1 (ROll) values(?)");
stm2.setInt(1,Roll);
k=stm2.executeUpdate();
javax.swing.JOptionPane.showMessageDialog(null,"Sucessfully Updated","Updation",javax.swing.JOptionPane.INFORMATION_MESSAGE);
TRollNo.setText("");
TName.setText("");
TAdd.setText("");
TAge.setText("");
CCourse.setSelectedIndex(-1);
CClass.setSelectedIndex(-1);
Hostel.setSelectedIndex(-1);
buttonGroup1.clearSelection();
}
catch(Exception e){
javax.swing.JOptionPane.showMessageDialog(null,"Cannot update","Error",javax.swing.JOptionPane.ERROR_MESSAGE);
}
}
}
Thanks
March 17, 2010 at 3:11 PM
Thanks a lot