FileDocCategorySizeDatePackage
JApplet.javaAPI DocJava SE 6 API18104Tue Jun 10 00:26:36 BST 2008javax.swing

JApplet

public class JApplet extends Applet implements RootPaneContainer, TransferHandler$HasGetTransferHandler, Accessible
An extended version of java.applet.Applet that adds support for the JFC/Swing component architecture. You can find task-oriented documentation about using JApplet in The Java Tutorial, in the section How to Make Applets.

The JApplet class is slightly incompatible with java.applet.Applet. JApplet contains a JRootPane as its only child. The contentPane should be the parent of any children of the JApplet. As a convenience add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

applet.add(child);
And the child will be added to the contentPane. The contentPane will always be non-null. Attempting to set it to null will cause the JApplet to throw an exception. The default contentPane will have a BorderLayout manager set on it. Refer to {@link javax.swing.RootPaneContainer} for details on adding, removing and setting the LayoutManager of a JApplet.

Please see the JRootPane documentation for a complete description of the contentPane, glassPane, and layeredPane properties.

Warning: Swing is not thread safe. For more information see Swing's Threading Policy.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

see
javax.swing.RootPaneContainer
beaninfo
attribute: isContainer true attribute: containerDelegate getContentPane description: Swing's Applet subclass.
version
1.71 08/08/06
author
Arnaud Weber

Fields Summary
protected JRootPane
rootPane
protected boolean
rootPaneCheckingEnabled
If true then calls to add and setLayout will be forwarded to the contentPane. This is initially false, but is set to true when the JApplet is constructed.
private TransferHandler
transferHandler
The TransferHandler for this applet.
protected AccessibleContext
accessibleContext
Constructors Summary
public JApplet()
Creates a swing applet instance.

This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale.

exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless
see
JComponent#getDefaultLocale


                                       
        
        super();
	// Check the timerQ and restart if necessary.
	TimerQueue q = TimerQueue.sharedInstance();
	if(q != null) {
	    synchronized(q) {
		if(!q.running)
		    q.start();
	    }
	}

	/* Workaround for bug 4155072.  The shared double buffer image
	 * may hang on to a reference to this applet; unfortunately 
	 * Image.getGraphics() will continue to call JApplet.getForeground()
	 * and getBackground() even after this applet has been destroyed.
	 * So we ensure that these properties are non-null here.
	 */
	setForeground(Color.black);
	setBackground(Color.white);

        setLocale( JComponent.getDefaultLocale() );
        setLayout(new BorderLayout());
        setRootPane(createRootPane());
        setRootPaneCheckingEnabled(true);
	
  	setFocusTraversalPolicyProvider(true);
        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
	
        enableEvents(AWTEvent.KEY_EVENT_MASK);
    
Methods Summary
protected voidaddImpl(java.awt.Component comp, java.lang.Object constraints, int index)
Adds the specified child Component. This method is overridden to conditionally forward calls to the contentPane. By default, children are added to the contentPane instead of the frame, refer to {@link javax.swing.RootPaneContainer} for details.

param
comp the component to be enhanced
param
constraints the constraints to be respected
param
index the index
exception
IllegalArgumentException if index is invalid
exception
IllegalArgumentException if adding the container's parent to itself
exception
IllegalArgumentException if adding a window to a container
see
#setRootPaneCheckingEnabled
see
javax.swing.RootPaneContainer

        if(isRootPaneCheckingEnabled()) {
            getContentPane().add(comp, constraints, index);
        }
        else {
            super.addImpl(comp, constraints, index);
        }
    
protected javax.swing.JRootPanecreateRootPane()
Called by the constructor methods to create the default rootPane.

        JRootPane rp = new JRootPane();
        // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there
        // is NO reason for the RootPane not to be opaque. For painting to
        // work the contentPane must be opaque, therefor the RootPane can
        // also be opaque.
        rp.setOpaque(true);
        return rp;
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this JApplet. For JApplets, the AccessibleContext takes the form of an AccessibleJApplet. A new AccessibleJApplet instance is created if necessary.

return
an AccessibleJApplet that serves as the AccessibleContext of this JApplet


                                                         
       
        if (accessibleContext == null) {
            accessibleContext = new AccessibleJApplet();
        }
        return accessibleContext;
    
public java.awt.ContainergetContentPane()
Returns the contentPane object for this applet.

see
#setContentPane
see
RootPaneContainer#getContentPane

 
        return getRootPane().getContentPane(); 
    
public java.awt.ComponentgetGlassPane()
Returns the glassPane object for this applet.

see
#setGlassPane
see
RootPaneContainer#getGlassPane

 
        return getRootPane().getGlassPane(); 
    
public java.awt.GraphicsgetGraphics()
{@inheritDoc}

since
1.6

        JComponent.getGraphicsInvoked(this);
        return super.getGraphics();
    
public javax.swing.JMenuBargetJMenuBar()
Returns the menubar set on this applet.

see
#setJMenuBar

        return getRootPane().getMenuBar();
    
public javax.swing.JLayeredPanegetLayeredPane()
Returns the layeredPane object for this applet.

exception
java.awt.IllegalComponentStateException (a runtime exception) if the layered pane parameter is null
see
#setLayeredPane
see
RootPaneContainer#getLayeredPane

 
        return getRootPane().getLayeredPane(); 
    
public javax.swing.JRootPanegetRootPane()
Returns the rootPane object for this applet.

see
#setRootPane
see
RootPaneContainer#getRootPane

 
        return rootPane; 
    
public javax.swing.TransferHandlergetTransferHandler()
Gets the transferHandler property.

return
the value of the transferHandler property
see
TransferHandler
see
#setTransferHandler
since
1.6

        return transferHandler;
    
protected booleanisRootPaneCheckingEnabled()
Returns whether calls to add and setLayout are forwarded to the contentPane.

return
true if add and setLayout are fowarded; false otherwise
see
#addImpl
see
#setLayout
see
#setRootPaneCheckingEnabled
see
javax.swing.RootPaneContainer

        return rootPaneCheckingEnabled;
    
protected java.lang.StringparamString()
Returns a string representation of this JApplet. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

return
a string representation of this JApplet.

	String rootPaneString = (rootPane != null ?
				 rootPane.toString() : "");
	String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
						"true" : "false");

	return super.paramString() +
	",rootPane=" + rootPaneString +
	",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
    
public voidremove(java.awt.Component comp)
Removes the specified component from the container. If comp is not the rootPane, this will forward the call to the contentPane. This will do nothing if comp is not a child of the JFrame or contentPane.

param
comp the component to be removed
throws
NullPointerException if comp is null
see
#add
see
javax.swing.RootPaneContainer

	if (comp == rootPane) {
	    super.remove(comp);
	} else {
	    getContentPane().remove(comp);
	}
    
public voidrepaint(long time, int x, int y, int width, int height)
Repaints the specified rectangle of this component within time milliseconds. Refer to RepaintManager for details on how the repaint is handled.

param
time maximum time in milliseconds before update
param
x the x coordinate
param
y the y coordinate
param
width the width
param
height the height
see
RepaintManager
since
1.6

        if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) {
            RepaintManager.currentManager(this).addDirtyRegion(
                              this, x, y, width, height);
        }
        else {
            super.repaint(time, x, y, width, height);
        }
    
public voidsetContentPane(java.awt.Container contentPane)
Sets the contentPane property. This method is called by the constructor.

param
contentPane the contentPane object for this applet
exception
java.awt.IllegalComponentStateException (a runtime exception) if the content pane parameter is null
see
#getContentPane
see
RootPaneContainer#setContentPane
beaninfo
hidden: true description: The client area of the applet where child components are normally inserted.

        getRootPane().setContentPane(contentPane);
    
public voidsetGlassPane(java.awt.Component glassPane)
Sets the glassPane property. This method is called by the constructor.

param
glassPane the glassPane object for this applet
see
#getGlassPane
see
RootPaneContainer#setGlassPane
beaninfo
hidden: true description: A transparent pane used for menu rendering.

        getRootPane().setGlassPane(glassPane);
    
public voidsetJMenuBar(javax.swing.JMenuBar menuBar)
Sets the menubar for this applet.

param
menuBar the menubar being placed in the applet
see
#getJMenuBar
beaninfo
hidden: true description: The menubar for accessing pulldown menus from this applet.

        getRootPane().setMenuBar(menuBar);
    
public voidsetLayeredPane(javax.swing.JLayeredPane layeredPane)
Sets the layeredPane property. This method is called by the constructor.

param
layeredPane the layeredPane object for this applet
see
#getLayeredPane
see
RootPaneContainer#setLayeredPane
beaninfo
hidden: true description: The pane which holds the various applet layers.

        getRootPane().setLayeredPane(layeredPane);
    
public voidsetLayout(java.awt.LayoutManager manager)
Sets the LayoutManager. Overridden to conditionally forward the call to the contentPane. Refer to {@link javax.swing.RootPaneContainer} for more information.

param
manager the LayoutManager
see
#setRootPaneCheckingEnabled
see
javax.swing.RootPaneContainer

        if(isRootPaneCheckingEnabled()) {
            getContentPane().setLayout(manager);
        }
        else {
            super.setLayout(manager);
        }
    
protected voidsetRootPane(javax.swing.JRootPane root)
Sets the rootPane property. This method is called by the constructor.

param
root the rootPane object for this applet
see
#getRootPane
beaninfo
hidden: true description: the RootPane object for this applet.

        if(rootPane != null) {
            remove(rootPane);
        }
        rootPane = root;
        if(rootPane != null) {
            boolean checkingEnabled = isRootPaneCheckingEnabled();
            try {
                setRootPaneCheckingEnabled(false);
                add(rootPane, BorderLayout.CENTER);
            }
            finally {
                setRootPaneCheckingEnabled(checkingEnabled);
            }
        }
    
protected voidsetRootPaneCheckingEnabled(boolean enabled)
Sets whether calls to add and setLayout are forwarded to the contentPane.

param
enabled true if add and setLayout are forwarded, false if they should operate directly on the JApplet.
see
#addImpl
see
#setLayout
see
#isRootPaneCheckingEnabled
see
javax.swing.RootPaneContainer
beaninfo
hidden: true description: Whether the add and setLayout methods are forwarded

        rootPaneCheckingEnabled = enabled;
    
public voidsetTransferHandler(javax.swing.TransferHandler newHandler)
Sets the {@code transferHandler} property, which is a mechanism to support transfer of data into this component. Use {@code null} if the component does not support data transfer operations.

If the system property {@code suppressSwingDropSupport} is {@code false} (the default) and the current drop target on this component is either {@code null} or not a user-set drop target, this method will change the drop target as follows: If {@code newHandler} is {@code null} it will clear the drop target. If not {@code null} it will install a new {@code DropTarget}.

Note: When used with {@code JApplet}, {@code TransferHandler} only provides data import capability, as the data export related methods are currently typed to {@code JComponent}.

Please see How to Use Drag and Drop and Data Transfer, a section in The Java Tutorial, for more information.

param
newHandler the new {@code TransferHandler}
see
TransferHandler
see
#getTransferHandler
see
java.awt.Component#setDropTarget
since
1.6
beaninfo
bound: true hidden: true description: Mechanism for transfer of data into the component

        TransferHandler oldHandler = transferHandler;
        transferHandler = newHandler;
        SwingUtilities.installSwingDropTargetAsNecessary(this, transferHandler);
        firePropertyChange("transferHandler", oldHandler, newHandler);
    
public voidupdate(java.awt.Graphics g)
Just calls paint(g). This method was overridden to prevent an unnecessary call to clear the background.

        paint(g);