Advertisement



< Prev
Next >



Swing Applets




So far, we have created the applets based on AWT(Abstract Window Toolkit) by extending the Applet class of the awt package. We can even create applets based on the Swing package. In order to create such applets, we must extend JApplet class of the swing package. JApplet extends Applet class, hence all the features of Applet class are available in JApplet as well, including JApplet's own Swing based features. Swing applets provides an easier to use user interface than AWT applets.




Some important points :


  • When we creat an AWT applet, we implement the paint(Graphics g) method to draw in it but when we create a Swing applet, we implement paintComponent(Graphics g) method to draw in it.
  • For an applet class that extends Swing's JApplet class, the way elements like textfield, buttons, checksboxes etc are added to this applet is performed by using a default layout manager, i.e. BorderLayout





Note:


JApplet is a top-level container class and you should never draw on it directly, instead you should draw on the component class like JPanel and then add this JPanel to the JApplet, as shown in the upcoming code.




  • Creating a swing applet


  • In the upcoming code, first, we have created a swing applet by extending JApplet class and we have added a JPanel to it.
  • Next, we have created a class B, which has extended JPanel class of Swing package and have also implemented ActionListener interace to listen to the button click event generated when buttons added to JPanel are clicked.

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

/* 
<applet code="Applet21" width="500" height="200">
</applet>
*/
  
public class Applet21 extends JApplet 
{

public void init()
{
add(new B());  	//Adding a JPanel to this Swing applet
}

}



class B extends JPanel implements ActionListener
{
JLabel jb;
JButton Box2, box2, box3, box4;
String str;

B()
{
jb= new JLabel("Welcome, please click on any button to unbox some interesting knowledge -");
Box2 = new JButton("Box2");
box2 = new JButton("Box2");
box3 = new JButton("Box3");
box4 = new JButton("Box4");

str ="";

setLayout(new FlowLayout());

add(jb);
add(Box2);
add(box2);
add(box3);
add(box4);

Box2.addActionListener(this);
box2.addActionListener(this);
box3.addActionListener(this);
box4.addActionListener(this);
}



public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Box2"))
{
str="Amazon is the largest tropical rain forest, covering 40% of the South America Continent.";
repaint();
}

if(ae.getActionCommand().equals("Box2"))
{
str="The Mariana Trench is the deepest point in Earth's ocean, with depth of over 10,994 metres.";
repaint();
}

if(ae.getActionCommand().equals("Box3"))
{
str="The coldest temperature ever recorded was -128.6F in Antarctica, on July 21, 1983.";
repaint();
}

if(ae.getActionCommand().equals("Box4"))
{
str="The oldest person to climb Mt. Everent was Japanese Miura Yiuchiro, at the age of 80.";
repaint();
}

}


public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString(str, 2, 170);
}

}


Output-



In the applet shown above, four buttons are presented in the output. These buttons are added to B class(which has extended JPanel). This JPanel is in turn added to our Swing applet class, Applet21. Whenever a button is clicked, an interesting fact about the world is presented to the user.


Advertisement




  • Creating another swing applet


  • In the next code, we have added four different buttons in different directions to our JPanel using BorderLayout layout manager. This JPanel is added to our Swing Applet, Applet19.

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /* 
    <applet code="Applet19" width="400" height="200">
    </applet>
    */
      
    public class Applet19 extends JApplet
    {
    public void init()
    {
    add(new MyClass()); 		//Adding JPanel to our Swing Applet.
    }
    }
    
    
    class MyClass extends JPanel implements ActionListener
    {
    JButton north, south, east, west;
    String str;
    
    MyClass()
    {
    
    north = new JButton("NORTH");
    south = new JButton("SOUTH");
    east = new JButton("EAST");
    west = new JButton("WEST");
    str ="";
    
    setLayout(new BorderLayout());
    
    add(north, BorderLayout.NORTH); 	//Adds button named north in the NORTH direction
    add(south, BorderLayout.SOUTH);		//Adds button named south in the SOUTH direction
    add(east, BorderLayout.EAST);		//Adds button named east in the EAST direction
    add(west, BorderLayout.WEST);		//Adds button named west in the WEST direction
    
    
    north.addActionListener(this);		//Registering MyClass to listen to event when button named north is pressed
    south.addActionListener(this);
    east.addActionListener(this);
    west.addActionListener(this);
    
    }
    
    
    
    public void actionPerformed(ActionEvent ae) 	//listening to button click events 
    {
    if(ae.getActionCommand().equals("NORTH"))		//when NORTH button is pressed
    {
    str="Are you going North?";
    repaint();
    }
    
    if(ae.getActionCommand().equals("SOUTH"))		//when SOUTH button is pressed
    {
    str="Are you going South?";
    repaint();
    }
    
    if(ae.getActionCommand().equals("EAST"))		//when EAST button is pressed
    {
    str="Are you going East?";
    repaint();
    }
    
    if(ae.getActionCommand().equals("WEST"))		//when WEST button is pressed
    {
    str="Are you going West?";
    repaint();
    }
    
    }
    
    
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    System.out.println("hello");
    System.out.println(str);
    g.drawString(str, 150, 150);
    }
    
    }


    First compile the above mentioned code Applet19.java.


    As you may see in the output Applet window, depending on which button is pressed, a specific message is displayed in the output window of Applet.



    Please share this article -



    Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement