Page 1

package myfiles; import java.awt.Color; import javax.swing.JOptionPane;

public class Validation extends javax.swing.JFrame {

public Validation() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { lblname = new javax.swing.JLabel(); txtname = new javax.swing.JTextField(); lblage = new javax.swing.JLabel(); txtage = new javax.swing.JTextField(); btnok = new javax.swing.JButton(); btnexit = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); txtarea = new javax.swing.JTextArea(); btnclear = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); lblname.setText("Name:"); txtname.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtnameActionPerformed(evt); } }); lblage.setText("Age:"); btnok.setText("Ok"); btnok.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnokActionPerformed(evt); } }); btnexit.setText("Exit"); btnexit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnexitActionPerformed(evt); } }); txtarea.setColumns(20); txtarea.setRows(5); jScrollPane1.setViewportView(txtarea); btnclear.setText("Clear"); btnclear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {


btnclearActionPerformed(evt); } }); 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(45, 45, 45) .addGroup(layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(btnok, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE) .addComponent(btnclear, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(48, 48, 48) .addComponent(btnexit, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblname) .addComponent(lblage)) .addGap(73, 73, 73) .addGroup(layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtage, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(46, 46, 46)) ); layout.setVerticalGroup( layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(39, 39, 39) .addGroup(layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblname) .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblage)


.addComponent(txtage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(42, 42, 42) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap (javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup (javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnok) .addComponent(btnexit) .addComponent(btnclear)) .addGap(65, 65, 65)) ); pack(); }// </editor-fold> private void btnexitActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } private void btnokActionPerformed(java.awt.event.ActionEvent evt) { String name = txtname.getText(); //Validation of an empty string field if(name.isEmpty()) { JOptionPane.showMessageDialog(null, "Your name field is empty, please enter " + "a name."); txtname.requestFocusInWindow(); txtname.setBackground(Color.RED); return; } String strage = txtage.getText(); if(strage.isEmpty()) { JOptionPane.showMessageDialog(null, "Your age field is empty, please enter " + "your age."); txtage.requestFocusInWindow(); txtage.setBackground(Color.RED); return; } int age = 0; try { age = Integer.parseInt(strage); } catch (NumberFormatException numberFormatException) { JOptionPane.showMessageDialog(null, "Please enter a numeric number."); txtage.requestFocusInWindow(); return; }


txtarea.append("Name is: " + name + '\n' + "Age is: " + age + '\n'); } private void txtnameActionPerformed(java.awt.event.ActionEvent evt) { } private void btnclearActionPerformed(java.awt.event.ActionEvent evt) { txtarea.setText(null); txtname.setText(null); txtage.setText(null); txtage.setBackground(Color.WHITE); txtname.setBackground(Color.WHITE); txtname.requestFocusInWindow(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Validation().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnclear; private javax.swing.JButton btnexit; private javax.swing.JButton btnok; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lblage; private javax.swing.JLabel lblname; private javax.swing.JTextField txtage; private javax.swing.JTextArea txtarea; private javax.swing.JTextField txtname; // End of variables declaration }


JavaW4validation1  

Victoria University

Advertisement
Read more
Read more
Similar to
Popular now
Just for you