Java
  Home arrow Java arrow Page 6 - 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: explanation


    (Page 6 of 6 )

    From the above sections, everybody knows that the following statement simply converts the value available in "txtFirst" to an integer and assigns the same to the variable "a."

    a = Integer.parseInt(this.txtFirst.getText());   

    If the user tries to provide an invalid value (or even a blank value), our program tends to crash. To solve this problem, I modified the above line with proper exception handling as follows:

      //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;
            }

    Even though the above code fragment looks a bit complicated, it is very simple to understand.  When it tries to execute the following statement:

            a = Integer.parseInt(this.txtFirst.getText());   

    it checks to see whether any runtime error occurred.  If no error occurred, it proceeds to the statement which is next to the "catch" block.  If any error occurs, it executes the following statements available in the "catch" block.

      this.txtFirst.setBackground(java.awt.Color.RED);
                this.txtFirst.requestFocus();
                return;

    The above statements would simply change the background color to RED and take the cursor to that control.  We have something similar set up for the variable "b" also. If the execution passes without any errors, it executes the following statements.

      //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));

    Any doubts, bugs, errors, suggestions, feedback etc. are highly appreciated at jag_chat@yahoo.com.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 2 hosted by Hostway
    Stay green...Green IT