Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Java - Swing Applet Example in java

                         

Introduction

In this section we will show you about using swing in an applet. In this example, you will see that how resources of swing are used in the applet. All objects except lbl, have been used from Applet class since lbl which inherits from the JLabel class of the javax.swing.*; package, this is swing label which shows the message in first time This is the Swing Applet Example. when the applet is loaded but again when you click on the Add button and if first text box is blank then the label lbl shows the message Invalid entry! in red color otherwise shows the message Output of the second Text Box : number_in_second_text_box.

Here is the code of the program : 

import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class SApplet extends Applet implements ActionListener {
  TextField input,output;
  Label label1,label2;
  Button b1;
  JLabel lbl;
  int num, sum = 0;
  public void init(){
    label1 = new Label("please enter number : ");
    add(label1);
    label1.setBackground(Color.yellow);
    label1.setForeground(Color.magenta);
    input = new TextField(5);
    add(input);
    label2 = new Label("Sum : ");
    add(label2);
    label2.setBackground(Color.yellow);
    label2.setForeground(Color.magenta);
    output = new TextField(20);
    add(output);
//    input.addActionListener( this );
    b1 = new Button("Add");
    add(b1);
    b1.addActionListener(this);
    lbl = new JLabel("This is the Swing Applet Example.          ");
    add(lbl);
    setBackground(Color.yellow);

//    output.addActionListener( this );
  }
  public void actionPerformed(ActionEvent ae){
    try{
//      num = Integer.parseInt(ae.getActionCommand());
      num = Integer.parseInt(input.getText());
      sum = sum+num;
      input.setText("");
      output.setText(Integer.toString(sum));
      lbl.setForeground(Color.blue);
      lbl.setText("Output of the second Text Box : " + output.getText());
    }
    catch(NumberFormatException e){
      lbl.setForeground(Color.red);
      lbl.setText("Invalid Entry!");
    }
  }
}

Here is the HTML code :

<HTML>
<BODY>
<applet code =
"SApplet" width = "260"height = "200"></applet>
</BODY>
</HTML>

Try Online this example.

Download this example.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

A very good example illustrating adding swing compnents to an Applet qnd demonstrating event handling technique.I shall use it for teaching introductory Java.Thanks
bputhal

Posted by bputhal on Tuesday, 03.13.07 @ 19:17pm | #11593

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.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2008. All rights reserved.