Class IFrame<T extends IFrame<T>>


public class IFrame<T extends IFrame<T>> extends StyledElement<T>
Represents the HTML <iframe> element for embedding external content.

The IFrame class provides a Java representation of the HTML inline frame element, which embeds another HTML page or external content within the current page. It creates a nested browsing context with security controls and dimension configuration.

Features:

  • External content embedding via URL
  • Inline HTML content via srcdoc attribute
  • Configurable width and height dimensions
  • Sandbox security restrictions
  • Named frame targeting for links
  • Independent browsing context
  • Cross-origin content isolation

Usage Example:


 // Create an iframe for external content
 IFrame externalFrame = new IFrame("https://example.com");
 externalFrame.setIntrinsicWidth(800);
 externalFrame.setIntrinsicHeight(600);
 externalFrame.setName("externalContent");

 // Create a sandboxed iframe
 IFrame sandboxedFrame = new IFrame();
 sandboxedFrame.setSrc("https://untrusted.com");
 sandboxedFrame.setSandbox(Sandbox.ALLOW_SCRIPTS);
 sandboxedFrame.setIntrinsicWidth(640);
 sandboxedFrame.setIntrinsicHeight(480);

 // Create an iframe with inline HTML
 IFrame inlineFrame = new IFrame();
 inlineFrame.setSrcDoc("<html><body><h1>Embedded Content</h1></body></html>");
 inlineFrame.setIntrinsicWidth(400);
 inlineFrame.setIntrinsicHeight(300);

 // Create a responsive iframe
 IFrame responsiveFrame = new IFrame("https://maps.google.com");
 responsiveFrame.setIntrinsicWidth(100); // Will be styled with CSS
 responsiveFrame.setIntrinsicHeight(450);
 responsiveFrame.setClass("responsive-iframe");
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • IFrame

      public IFrame()
      Creates a new empty IFrame element.
    • IFrame

      public IFrame(String url)
      Creates a new IFrame element with the specified source URL.
      Parameters:
      url - The URL of the page to embed.
  • Method Details

    • setName

      public final T setName(String name)
      Sets the name attribute, which can be used as a target for links and forms.
      Parameters:
      name - The name for this iframe's browsing context.
    • setAllow

      public final T setAllow(String allow)
      Sets the allow attribute specifying a permissions policy for the embedded content.

      Controls which features (e.g., camera, microphone, geolocation) are available to the iframe content.

      Parameters:
      allow - The permissions policy for the iframe.
      Returns:
      This element for method chaining.
    • setAllowFullScreen

      public final T setAllowFullScreen(boolean allowFullScreen)
      Sets the allowfullscreen boolean attribute.

      Allows the embedded content to request fullscreen mode.

      Parameters:
      allowFullScreen - True to allow fullscreen, false to remove.
      Returns:
      This element for method chaining.
    • setSandbox

      public final T setSandbox(Sandbox sandbox)
      Sets the iframe sandbox restriction using the Sandbox enum.
      Parameters:
      sandbox - The sandbox restriction.
      Returns:
      This element for method chaining.
    • setSandbox

      public final T setSandbox(String sandbox)
      Sets the sandbox attribute applying extra restrictions to the iframe content.

      When present with no value, all restrictions apply. Space-separated tokens can selectively allow specific capabilities (e.g., "allow-scripts", "allow-forms", "allow-same-origin").

      Parameters:
      sandbox - Space-separated sandbox tokens.
      Returns:
      This element for method chaining.
    • setSrc

      public final T setSrc(String src)
      Sets the URL of the page to embed.

      If the URL starts with "/" (absolute path within the application), the servlet context path will be automatically prepended.

      Parameters:
      src - The source URL for the iframe content.
    • setSrcDoc

      public final T setSrcDoc(String html)
      Sets the srcdoc attribute specifying inline HTML content for the iframe.

      Overrides the src attribute. The value is a complete HTML document string that is rendered within the iframe's browsing context.

      Parameters:
      html - The HTML content for the iframe.
      Returns:
      This element for method chaining.
    • setIntrinsicWidth

      public T setIntrinsicWidth(int pixels)
      Sets the HTML width attribute to specify intrinsic width in pixels.

      This sets the HTML attribute (e.g., <img width="300">), not the CSS width property. To set CSS width, use the inherited StyledElement.setWidth(int) method.

      Parameters:
      pixels - The width in pixels.
      Returns:
      This element for method chaining.
    • setIntrinsicWidth

      public T setIntrinsicWidth(String width)
      Sets the HTML width attribute to specify intrinsic width.

      This sets the HTML attribute, not the CSS width property. To set CSS width, use the inherited StyledElement.setWidth(String) method.

      Parameters:
      width - The width value.
      Returns:
      This element for method chaining.
    • setIntrinsicHeight

      public T setIntrinsicHeight(int pixels)
      Sets the HTML height attribute to specify intrinsic height in pixels.

      This sets the HTML attribute (e.g., <img height="200">), not the CSS height property. To set CSS height, use the inherited StyledElement.setHeight(int) method.

      Parameters:
      pixels - The height in pixels.
      Returns:
      This element for method chaining.
    • setIntrinsicHeight

      public T setIntrinsicHeight(String height)
      Sets the HTML height attribute to specify intrinsic height.

      This sets the HTML attribute, not the CSS height property. To set CSS height, use the inherited StyledElement.setHeight(String) method.

      Parameters:
      height - The height value.
      Returns:
      This element for method chaining.
    • getIntrinsicWidth

      public String getIntrinsicWidth()
      Returns the HTML width attribute value.

      Returns the intrinsic width set via the HTML attribute, not the CSS width.

      Returns:
      The width value, or null if not set.
    • getIntrinsicHeight

      public String getIntrinsicHeight()
      Returns the HTML height attribute value.

      Returns the intrinsic height set via the HTML attribute, not the CSS height.

      Returns:
      The height value, or null if not set.