Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Sum of a Number using Swing 
 

In this section, you will learn how to sum of a number using swing. JTextField, JButton is a component of GUI.

 

Sum of a Number using Swing

                         

In this section, you will learn how to sum of a number using swing. JTextField, JButton is a component of GUI.

Brief description of the Component used in this application :

1. JFrame is used to  creating  java standalone application, you must provide GUI for a user. The most common way of creating a frame is, 
using single argument constructor of the JFrame class. The argument of the constructor is the title of the window or frame.

           JFrame f = new JFrame("Calculate");

2. JTextField is a lightweight component that allows the editing of a single line of text. JTextField has a method to establish the string 
used as the command string for the action event that gets fired. The java.awt.TextField used the text of the field as the command string 
or the ActionEvent. JTextField will use the command string set with the setActionCommand method if not null, otherwise it will use the
 text of the field as a compatibility with java.awt.TextField. 

     final JTextField  text1 = new JTextField(20);
   final JTextField text2 = new JTextField(20);

3. JButton with a String as a label, and then drop it in a window. Events are normally handled just as with a Button: you attach an 
ActionListener via the addActionListener method. 

    JButton button = new JButton("Calculate");

Step: Create a file "Calculate.java" to sum of a number using Swing.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class Calculate extends JTextField {

  public static void main(String[] args) {
    JFrame f = new JFrame("Calculate");
    f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), 
    BoxLayout.Y_AXIS));

    final JTextField  text1 = new JTextField(20);
  

    final JTextField text2 = new JTextField(20);
  

    JPanel panel = new JPanel();
    JButton button = new JButton("Calculate");
    panel.add(button);
    f.getContentPane().add(text1);
    f.getContentPane().add(panel);
    f.getContentPane().add(text2);
    
  ActionListener l = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        System.out.println("Action event from a text field");
      }
    };
    text1.addActionListener(l);
  text2.addActionListener(l);
  button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  int a=Integer.parseInt(text1.getText()); 
  int sum = 0;
  int count=1;
  while(count<=a)
  {
    sum+=count;
    count++;
  }
    text2.setText(String.valueOf(sum));
  }
    });
    f.pack();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Output :

Download the full Code

                         

» View all related tutorials
Related Tags: c memory image class images interface io colors sed color get implementation display icon height value source width int id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.