Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Java Example Java Applet Swing Applet Example in java

Related Tutorials


Share on Google+Share on Google+

Swing Applet Example in java

In this section we will show you about using swing in an applet.

Java - Swing Applet Example in java



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 : ");
  input = new TextField(5);
  label2 = new Label("Sum : ");
  output = new TextField(20);
//  input.addActionListener( this );
  b1 = new Button("Add");
  lbl = new JLabel("This is the Swing Applet Example.  ");

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

Here is the HTML code :

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

Try Online this example.

Download this example.


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Discuss: Swing Applet Example in java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
May 22, 2012

thank you