Open In App

How to Use Swing Applet in Java?

Last Updated : 23 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be using the swing Applet or JApplet in Java. Here, we will make a simple multiplication application that will multiply the two input numbers.

Approach to Using Swing Applet in Java

  • We need to import the packages for Swing and AWT Components.
  • Once the packages are imported, we need to create the class that implements the ActionListener interface.
  • Then we need to initialize all the required GUI components like labels, buttons, and input fields.
  • We will create the JPanel to align and arrange all the GUI components in the Grid layout.
  • Then we will set up the event listener for registering the applet as the action listener for the Multiply button.
  • We will handle the button click in the actionPerformed method to perform the multiply operation and show the result.
  • We will make the HTML file to run the applet.
  • We have to compile the Java code using Javac and then we need to run using appletviewer.

Steps for Creating and Running the Swing Applet

The folder view of the project is shown in the below image.

Folder Structure for application

Step 1: First, create the file as SApplet.java and enter the code into it.

Step 2: Once the code is been inserted then we need to create the SApplet.html file, through which we will display the output.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Java Applet Example</title>
</head>
<body>
    <applet code="SApplet.class" width=220 height=90>
        No Support
    </applet>
</body>
</html>


Step 3: Then we need to compile the Java code using the below command:

javac SApplet.java

Step 4: Now finally we need to run the application using the below power:

appletviewer SApplet.html

Program using SApplet

Implementation of the SApplet program is mentioned below:

Java




// Java Program for Using Swing Applet in Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
// Defining a class named SApplet that extends JApplet and
// implements the ActionListener interface
public class SApplet extends JApplet implements ActionListener {
    JTextField num1, num2, res;
    JButton resBtn;
  
    // This is Initialization method for the applet
    public void init()
    {
        // Creating three text fields for number input and
        // one button for calculation result
        num1 = new JTextField(5);
        num2 = new JTextField(5);
        res = new JTextField(10);
  
        // Make the result field non-editable
        res.setEditable(false);
        resBtn = new JButton("Multiply");
  
        // Register this applet as the action
        // listener for the button
        resBtn.addActionListener(this);
  
        // Creating labels for the input fields and result
        JLabel l1 = new JLabel("Enter Number 1:");
        JLabel l2 = new JLabel("Enter Number 2:");
        JLabel l3 = new JLabel("Result:");
  
        l1.setForeground(Color.black);
        l2.setForeground(Color.black);
        l3.setForeground(Color.black);
  
        // Creating a JPanel to arrange
        // components in a grid layout
        JPanel pan = new JPanel();
        pan.setLayout(new GridLayout(4, 2));
  
        // Adding labels, input fields, button, and empty
        // label for spacing
        pan.add(l1);
        pan.add(num1);
        pan.add(l2);
        pan.add(num2);
  
        // Set button background color
        resBtn.setBackground(Color.orange);
        pan.add(resBtn);
  
        // Empty label for spacing
        pan.add(new JLabel());
        pan.add(l3);
        pan.add(res);
  
        // Set text color Customize the fonts of various
        // components
        pan.setForeground(Color.black);
        Font lfont = l1.getFont();
  
        // Creating a new font with size 20
        Font inFont = lfont.deriveFont(Font.PLAIN, 20);
  
        l1.setFont(inFont);
        l2.setFont(inFont);
        l3.setFont(inFont);
  
        num1.setFont(inFont);
        num2.setFont(inFont);
  
        res.setFont(inFont);
        resBtn.setFont(inFont);
  
        // Set the layout of the applet to BorderLayout and
        // add the JPanel to the center
        setLayout(new BorderLayout());
        add(pan, BorderLayout.CENTER);
    }
  
    // ActionListener method to handle button clicks
    public void actionPerformed(ActionEvent e){
        
        // Check if the event source is the Multiply button
        if (e.getSource() == resBtn) {
  
            String str1 = num1.getText();
            String str2 = num2.getText();
  
            try {
                int num1 = Integer.parseInt(str1);
                int num2 = Integer.parseInt(str2);
                int result = num1 * num2;
  
                // Display the result in result text field
                res.setText(Integer.toString(result));
            }
            catch (NumberFormatException ex) {
  
                // Display an error message for invalid input
                res.setText("Wrong Input");
            }
        }
    }
}


Output for the Program:

Applet

Explanation of the Program

  • We have imported the packages which are required to create the GUI interface of the application.
  • Then we have defined the class called as ‘SApplet’ which mainly enters the ‘JApplet’ and implements the ‘ActionListener’ interface.
  • There are instance variables like num1 and num2 for text fields, resBtn is the button to perform the multiplication.
  • There is an init method which is called when the application is been initialized. We are creating the text fields, lbales, JPanel using this method.
  • For customization purposes, we have used the Font sizes and colors applied on labels and input fields. Also, we have designed the JPanel pan, to manage the components properly in the grid manner.
  • Then there is the actionPerformed() method, which mainly is called when the action on the button is been done. Here the text from the text fields num1 and num2 is been returned and the result is been displayed in the res text field of multiplication.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads