Home | | Web Programming | Create a Swing Applet

Chapter: Java The Complete Reference : Introducing GUI Programming with Swing : Introducing Swing

Create a Swing Applet

The second type of program that commonly uses Swing is the applet. Swing-based applets are similar to AWT-based applets, but with an important difference: A Swing applet extends JApplet rather than Applet. JApplet is derived from Applet.

Create a Swing Applet

 

The second type of program that commonly uses Swing is the applet. Swing-based applets are similar to AWT-based applets, but with an important difference: A Swing applet extends JApplet rather than Applet. JApplet is derived from Applet. Thus, JApplet includes all of the functionality found in Applet and adds support for Swing. JApplet is a top-level Swing container, which means that it is not derived from JComponent. Because JApplet is a top-level container, it includes the various panes described earlier. This means that all components are added to JApplet’s content pane in the same way that components are added to JFrame’s content pane.

 

Swing applets use the same four life-cycle methods as described in Chapter 23: init( ), start( ), stop( ), and destroy( ). Of course, you need override only those methods that are needed by your applet. Painting is accomplished differently in Swing than it is in the AWT, and a Swing applet will not normally override the paint( ) method. (Painting in Swing is described later in this chapter.)


One other point: All interaction with components in a Swing applet must take place on the event dispatching thread, as described in the previous section. This threading issue applies to all Swing programs.

Here is an example of a Swing applet. It provides the same functionality as the previous application, but does so in applet form. Figure 31-3 shows the program when executed by appletviewer.

 

// A simple Swing-based applet

 

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

 

/*

 

This HTML can be used to launch the applet:

 

<applet code="MySwingApplet" width=220 height=90> </applet>

 

*/

 

public class MySwingApplet extends JApplet { JButton jbtnAlpha;

 

JButton jbtnBeta;

 

JLabel jlab;

 

// Initialize the applet.

public void init() {

 

try {

 

SwingUtilities.invokeAndWait(new Runnable () { public void run() {

 

makeGUI(); // initialize the GUI

 

}

 

});

 

} catch(Exception exc) {

 

System.out.println("Can’t create because of "+ exc);

 

}

 

}

     //This applet does not need to override start(), stop(),

 

     //or destroy().

 

     //Set up and initialize the GUI.

 

private void makeGUI() {

 

     //Set the applet to use flow layout.

     setLayout(new FlowLayout());

 

     //Make two buttons.

 

jbtnAlpha = new JButton("Alpha"); jbtnBeta = new JButton("Beta");

 

     Add action listener for Alpha. jbtnAlpha.addActionListener(new ActionListener() {

 

public void actionPerformed(ActionEvent le) { jlab.setText("Alpha was pressed.");

 

}

 

});

 

     //Add action listener for Beta.

     jbtnBeta.addActionListener(new ActionListener() {

 

public void actionPerformed(ActionEvent le) { jlab.setText("Beta was pressed.");

 

}

 

});

 

     //Add the buttons to the content pane.

     add(jbtnAlpha);

 

add(jbtnBeta);

 

     //Create a text-based label.

 

jlab = new JLabel("Press a button.");

 

// Add the label to the content pane.

add(jlab);

 

}

 

}

 

There are two important things to notice about this applet. First, MySwingApplet extends JApplet. As explained, all Swing-based applets extend JApplet rather than Applet. Second, the init( ) method initializes the Swing components on the event dispatching thread by setting up a call to makeGUI( ). Notice that this is accomplished through the use of invokeAndWait( ) rather than invokeLater( ). Applets must use invokeAndWait( ) because the init( ) method must not return until the entire initialization process has been completed. In essence, the start( ) method cannot be called until after initialization, which means that the GUI must be fully constructed.

 

Inside makeGUI( ), the two buttons and label are created, and the action listeners are added to the buttons. Finally, the components are added to the content pane. Although this example is quite simple, this same general approach must be used when building any Swing GUI that will be used by an applet.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : Introducing GUI Programming with Swing : Introducing Swing : Create a Swing Applet |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.