Total members 11894 |It is currently Sun Nov 24, 2024 1:36 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





hi good morning everybody

I am doing a java project,I want to handle all the exception in a single
class,wherever the exception raises ,I want to handle it in a class
can anyone send some sample for this ,




Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

Even it's just one class, you still need to use interface for that. Each interface will have its own listener class which will override all the methods. This is one sample code that you could try to analyze. Hope it's helpful :)
Code:
//problem statement 5.P.1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//import java.awt.Container;

public class MyDealer extends JApplet implements KeyListener
{
   //variable for the panel
   JPanel panelObject;

   //variables for labels
   JLabel labelCustName;
   JLabel labelCustCellNo;
   JLabel labelCustAddress;
   JLabel labelCustAge;

   //variables for data entry controls
   JTextField textCustName;
   JTextField textCustCellNo;
   JTextField textAddress;
   JTextField textCustAge;

   String name, no, add, age;

   JButton btnSubmit;

   //variables for the layout
   GridBagLayout gbObject;
   GridBagConstraints gbc;

   public void init()
   {
      //initializes the layout variables
      gbObject = new GridBagLayout();
      gbc = new GridBagConstraints();
      panelObject = (JPanel)getContentPane();
      panelObject.setLayout(gbObject);

      setSize(800,200);

      //initializes label controls
      labelCustName = new JLabel("Dealer Name");
      labelCustCellNo = new JLabel("Cell Number ");
      labelCustAddress= new JLabel("Address");
      labelCustAge = new JLabel("Age");

      //initializes data entry controls
      textCustName = new JTextField(30);
      textCustCellNo = new JTextField(15);
      textCustAge = new JTextField(2);
      textAddress = new JTextField(45);
      btnSubmit = new JButton("SUBMIT");


      //Add listeners to the controls
      textCustName.addKeyListener(this);
      textCustCellNo.addKeyListener(this);
      textAddress.addKeyListener(this);
      textCustAge.addKeyListener(this);

      //add controls for customer name;
      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx = 1;
      gbc.gridy = 5;
      gbObject.setConstraints(labelCustName,gbc);
      panelObject.add(labelCustName);

      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx = 4;
      gbc.gridy = 5;
      gbObject.setConstraints(textCustName,gbc);
      panelObject.add(textCustName);

      //add controls for cell number
      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =1;
      gbc.gridy =8;
      gbObject.setConstraints(labelCustCellNo,gbc);
      panelObject.add(labelCustCellNo);

      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =4;
      gbc.gridy =8;
      gbObject.setConstraints(textCustCellNo, gbc);
      panelObject.add(textCustCellNo);

      //add controls for package
      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =1;
      gbc.gridy =11;
      gbObject.setConstraints(labelCustAddress, gbc);
      panelObject.add(labelCustAddress);

      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =4;
      gbc.gridy =11;
      gbObject.setConstraints(textAddress, gbc);
      panelObject.add(textAddress);

      //add controls for customer age
      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =1;
      gbc.gridy =14;
      gbObject.setConstraints(labelCustAge, gbc);
      panelObject.add(labelCustAge);

      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =4;
      gbc.gridy =14;
      gbObject.setConstraints(textCustAge, gbc);
      panelObject.add(textCustAge);

      gbc.anchor = GridBagConstraints.SOUTH;
      gbc.gridx = 4;
      gbc.gridy = 16;
      gbObject.setConstraints(btnSubmit,gbc);
      panelObject.add(btnSubmit);


   }

   // BEGINNING OF KEY LISTENER FUNCTIONS
   public void keyPressed(KeyEvent e)
   {
      Object obj = e.getSource();
         if (obj == textCustName)
         {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER)
            {
               String name = textCustName.getText();
               if (name.length() == 0)
               {
                  JOptionPane.showMessageDialog(this,"Name cannot be empty");
                  //showStatus("cannot be empty");
                  //return;
               }
               else
               {
                  textCustCellNo.requestFocusInWindow();
               }
            }
         }

         if (obj == textCustCellNo)
         {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER)
            {
               String no = textCustCellNo.getText();
               if (no.length() != 10)
               {
                  JOptionPane.showMessageDialog(this,"Mobile number entered should be of ten digits");
               }
               else
               {
                  textAddress.requestFocusInWindow();
               }
            }
         }

         if (obj == textAddress)
         {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER)
            {
                  textCustAge.requestFocusInWindow();
            }
         }

         if (obj == textCustAge)
         {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER)
            {
               int age = Integer.parseInt(textCustAge.getText());
               if (age <= 18 || age >= 100)
               {
                  JOptionPane.showMessageDialog(this,"Wrong Age Input. Please Recheck!");
               }
               else
               {
                  btnSubmit.requestFocusInWindow();
               }
            }
         }
   }
   public void keyReleased(KeyEvent e)
   {
   }
   public void keyTyped(KeyEvent e)
   {


      //showStatus(" Recently typed characters are : " + e.getKeyChar());
   }
   //END OF KEY LISTENER FUNCTIONS
}





Author:
Newbie
User avatar Posts: 16
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Exception Handling in a project
 how to design a exception handling for a project     -  
 Exception handling     -  
 exception handling try and catch in Python     -  
 Out of Bounds exception     -  
 Exception is not clear     -  
 Exception Handler in java     -  
 Define your own exception class     -  
 String Too Long Exception     -  
 Multiple Exception Catching     -  
 java.security Exception in Applet     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com