Java
  Home arrow Java arrow Page 5 - Working with JFC/Swing Controls using NetB...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Working with JFC/Swing Controls using NetBeans IDE
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2006-05-10

    Table of Contents:
  • Working with JFC/Swing Controls using NetBeans IDE
  • Working with text boxes, buttons, labels: understanding the code
  • Working with text boxes, buttons, labels: understanding the code continued
  • How about the JButton and its events?
  • Introducing error handling or exception handling in Java using NetBeans IDE
  • Introducing error handling or exception handling in Java using NetBeans IDE: explanation

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with JFC/Swing Controls using NetBeans IDE - Introducing error handling or exception handling in Java using NetBeans IDE


    (Page 5 of 6 )

    The above example that I presented works well if you provide the values correctly. The application may crash if you try to provide some incorrect values (or even no values).  To handle such situations, we can use the exception handling built within Java.

    In this section, I shall only introduce exception handling (with respect to the previous example). In my upcoming articles, we shall go in an in-depth discussion of exceptions. Now let us go through the modified code as follows:

      private void btnSumActionPerformed(java.awt.event.ActionEvent evt) {                                      
      // TODO add your handling code here:
            int a=0,b=0;       

            //check for the validity of first number
            try
            {
                a = Integer.parseInt(this.txtFirst.getText());    
            }
            catch(Exception e)
            {
                this.txtFirst.setBackground(java.awt.Color.RED);
                this.txtFirst.requestFocus();
                return;
            }       

            //check for the validity of second number
            try
            {
                b = Integer.parseInt(this.txtSecond.getText());
            }
            catch(Exception e)
            {
                this.txtSecond.setBackground(java.awt.Color.RED);
                this.txtSecond.requestFocus();
                return;
            }       

            //set back the colors and give the result
            this.txtFirst.setBackground(java.awt.Color.WHITE);
            this.txtSecond.setBackground(java.awt.Color.WHITE);
            this.lblResult.setText(String.valueOf(a+b));
        }                

    The next section will explain the above code in detail.

    More Java Articles
    More By Jagadish Chaterjee


       · Hello guys. Another article added on NetBeans IDE. enjoy :)
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT