Java
  Home arrow Java arrow Page 4 - 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 
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 - How about the JButton and its events?


    (Page 4 of 6 )

    In the previous section, I didn't really specify anything about "btnSum."  I shall give you a detailed explanation in this section. Let us go through the code generated automatically by IDE for the button:

      btnSum.setText("Sum");
            btnSum.addActionListener(new
                     java.awt.event.ActionListener() {
                public void actionPerformed(
                       java.awt.event.ActionEvent evt) {
                    btnSumActionPerformed(evt);
                }
            });
     
    getContentPane().add(btnSum);
      btnSum.setBounds(180, 80, 80, 23);

    Now you can see that working with "btnSum" is a bit different from working with the others (because it has something to do when the user clicks).  In general, every control/component may expose several events. Our application needs to "listen" to those events when necessary. 

    For example, "JButton" exposes the "actionPerformed" event, helping you to execute your own code when the button is clicked.  To work with that event, we need to "listen" (or add a listener based on "actionPerformed") to that event. The following code fragment adds a listener with respect to "btnSum."

      btnSum.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(
                      java.awt.event.ActionEvent evt) {
                    btnSumActionPerformed(evt);
                }
            });

    In short, it executes the method "btnSumActionPerformed" when the button is hit.  Let us see how "btnSumActionPerformed" is framed:

      private void btnSumActionPerformed(java.awt.event.ActionEvent evt) {                                      
      // TODO add your handling code here:
            int a = Integer.parseInt(this.txtFirst.getText());
            int b = Integer.parseInt(this.txtSecond.getText());
            this.lblResult.setText(String.valueOf(a+b));
        }   

    The method gets executed whenever the "btnSum" is clicked.  The first statement within the method gets the text available in "txtFirst," converts it to an integer and finally assigns to it the variable "a."  The second statement within the method gets the text available in "txtSecond," converts it to an integer and finally assigns to it the variable "b."  Finally, in the third statement, I am adding the two values (present in "a" and "b"), converting to string and finally placing it into "lblResult."

    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-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT