Class RelativeLayout

  • All Implemented Interfaces:
    java.awt.LayoutManager, java.awt.LayoutManager2, java.io.Serializable

    public class RelativeLayout
    extends java.lang.Object
    implements java.awt.LayoutManager2, java.io.Serializable
    The RelativeLayout class is a layout manager that lays out a container's components on the specified X or Y axis. Components can be layed out at their preferred size or at a relative size. When relative sizing is used the component must be added to the container using a relative size constraint, which is simply a Float value. The space available for relative sized components is determined by subtracting the preferred size of the other components from the space available in the container. Each component is then assigned a size based on its relative size value. For example: container.add(component1, new Float(1)); container.add(component2, new Float(2)); There is a total of 3 relative units. If the container has 300 pixels of space available then component1 will get 100 and component2, 200. It is possible that rounding errors will occur in which case you can specify a rounding policy to use to allocate the extra pixels. By defaults components are center aligned on the secondary axis however this can be changed at the container or component level.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      RelativeLayout()
      Creates a relative layout with the components layed out on the X-Axis using the default gap
      RelativeLayout​(int axis)
      Creates a relative layout with the components layed out on the specified axis using the default gap
      RelativeLayout​(int axis, int gap)
      Creates a relative layout with the components layed out on the specified axis using the specfied gap
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addLayoutComponent​(java.awt.Component component, java.lang.Object constraint)  
      void addLayoutComponent​(java.lang.String name, java.awt.Component component)
      Not supported
      protected void adjustForRounding​(int[] relativeSpace, int spaceRemaining)  
      float getAlignment()
      Gets the alignment of the components on the opposite axis.
      int getAxis()
      Gets the layout axis.
      int getBorderGap()
      Gets the initial gap.
      java.lang.Float getConstraints​(java.awt.Component component)
      Gets the constraints for the specified component.
      int getFillGap()
      Gets the fill gap amount.
      int getGap()
      Gets the gap between components.
      float getLayoutAlignmentX​(java.awt.Container parent)
      Returns the alignment along the x axis.
      float getLayoutAlignmentY​(java.awt.Container parent)
      Returns the alignment along the y axis.
      int getRoundingPolicy()
      Gets the rounding policy.
      void invalidateLayout​(java.awt.Container target)
      Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
      boolean isFill()
      Gets the fill property for the component size on the opposite edge.
      void layoutContainer​(java.awt.Container parent)
      Lays out the specified container using this layout.
      java.awt.Dimension maximumLayoutSize​(java.awt.Container target)
      There is no maximum.
      java.awt.Dimension minimumLayoutSize​(java.awt.Container parent)
      Determines the minimum size of the container argument using this column layout.
      java.awt.Dimension preferredLayoutSize​(java.awt.Container parent)
      Determines the preferred size of the container argument using this column layout.
      void removeLayoutComponent​(java.awt.Component comp)
      Removes the specified component from the layout.
      void setAlignment​(float alignment)  
      void setAxis​(int axis)
      Sets the layout axis
      void setBorderGap​(int borderGap)
      Sets the initial gap.
      void setFill​(boolean fill)
      Change size of relative components to fill the space available For X-AXIS aligned components the height will be filled.
      void setFillGap​(int fillGap)
      Specify the number of pixels by which the fill size is decreased when setFill(true) has been specified.
      void setGap​(int gap)
      Sets the gap between components to the specified value.
      void setRoundingPolicy​(int roundingPolicy)
      Specify the rounding policy to be used when all the avialable pixels have not been allocated to a component.
      java.lang.String toString()
      Returns the string representation of this column layout's values.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • RelativeLayout

        public RelativeLayout()
        Creates a relative layout with the components layed out on the X-Axis using the default gap
      • RelativeLayout

        public RelativeLayout​(int axis)
        Creates a relative layout with the components layed out on the specified axis using the default gap

        Parameters:
        axis - X-AXIS or Y_AXIS
      • RelativeLayout

        public RelativeLayout​(int axis,
                              int gap)
        Creates a relative layout with the components layed out on the specified axis using the specfied gap

        All RelativeLayout constructors defer to this one.

        Parameters:
        axis - X-AXIS or Y_AXIS
        gap - the gap
    • Method Detail

      • getAxis

        public int getAxis()
        Gets the layout axis.
        Returns:
        the layout axis
      • setAxis

        public void setAxis​(int axis)
        Sets the layout axis
        Parameters:
        axis - the layout axis
      • getGap

        public int getGap()
        Gets the gap between components.
        Returns:
        the gap between components
      • setGap

        public void setGap​(int gap)
        Sets the gap between components to the specified value.
        Parameters:
        gap - the gap between components
      • getBorderGap

        public int getBorderGap()
        Gets the initial gap. This gap is used before the leading component and after the trailing component.
        Returns:
        the leading/trailing gap
      • setBorderGap

        public void setBorderGap​(int borderGap)
        Sets the initial gap. This gap is used before the leading component and after the trailing component. The default is set to the gap.
        Parameters:
        borderGap - the leading/trailing gap
      • getAlignment

        public float getAlignment()
        Gets the alignment of the components on the opposite axis.
        Returns:
        the alignment
      • setAlignment

        public void setAlignment​(float alignment)
      • isFill

        public boolean isFill()
        Gets the fill property for the component size on the opposite edge.
        Returns:
        the fill property
      • setFill

        public void setFill​(boolean fill)
        Change size of relative components to fill the space available For X-AXIS aligned components the height will be filled. For Y-AXIS aligned components the width will be filled.
      • getFillGap

        public int getFillGap()
        Gets the fill gap amount.
        Returns:
        the fill gap value
      • setFillGap

        public void setFillGap​(int fillGap)
        Specify the number of pixels by which the fill size is decreased when setFill(true) has been specified.
      • getRoundingPolicy

        public int getRoundingPolicy()
        Gets the rounding policy.
        Returns:
        the rounding policy
      • setRoundingPolicy

        public void setRoundingPolicy​(int roundingPolicy)
        Specify the rounding policy to be used when all the avialable pixels have not been allocated to a component. DO_NOTHING FIRST - extra pixels added to the first relative component LAST - extra pixels added to the last relative component LARGEST (default) - extra pixels added to the larger relative component EQUAL - a single pixel is added to each relative component (until pixels are used up)
      • getConstraints

        public java.lang.Float getConstraints​(java.awt.Component component)
        Gets the constraints for the specified component.
        Parameters:
        component - the component to be queried
        Returns:
        the constraint for the specified component, or null if component is null or is not present in this layout
      • addLayoutComponent

        public void addLayoutComponent​(java.lang.String name,
                                       java.awt.Component component)
        Not supported
        Specified by:
        addLayoutComponent in interface java.awt.LayoutManager
      • addLayoutComponent

        public void addLayoutComponent​(java.awt.Component component,
                                       java.lang.Object constraint)
        Specified by:
        addLayoutComponent in interface java.awt.LayoutManager2
      • removeLayoutComponent

        public void removeLayoutComponent​(java.awt.Component comp)
        Removes the specified component from the layout.
        Specified by:
        removeLayoutComponent in interface java.awt.LayoutManager
        Parameters:
        comp - the component to be removed
      • preferredLayoutSize

        public java.awt.Dimension preferredLayoutSize​(java.awt.Container parent)
        Determines the preferred size of the container argument using this column layout.

        The preferred width of a column layout is the largest preferred width of each column in the container, plus the horizontal padding times the number of columns minus one, plus the left and right insets of the target container.

        The preferred height of a column layout is the largest preferred height of each row in the container, plus the vertical padding times the number of rows minus one, plus the top and bottom insets of the target container.

        Specified by:
        preferredLayoutSize in interface java.awt.LayoutManager
        Parameters:
        target - the container in which to do the layout
        Returns:
        the preferred dimensions to lay out the subcomponents of the specified container
        See Also:
        java.awt.RelativeLayout#minimumLayoutSize, Container.getPreferredSize()
      • minimumLayoutSize

        public java.awt.Dimension minimumLayoutSize​(java.awt.Container parent)
        Determines the minimum size of the container argument using this column layout.

        The minimum width of a grid layout is the largest minimum width of each column in the container, plus the horizontal padding times the number of columns minus one, plus the left and right insets of the target container.

        The minimum height of a column layout is the largest minimum height of each row in the container, plus the vertical padding times the number of rows minus one, plus the top and bottom insets of the target container.

        Specified by:
        minimumLayoutSize in interface java.awt.LayoutManager
        Parameters:
        target - the container in which to do the layout
        Returns:
        the minimum dimensions needed to lay out the subcomponents of the specified container
        See Also:
        java.awt.RelativeLayout#preferredLayoutSize, Container.doLayout()
      • layoutContainer

        public void layoutContainer​(java.awt.Container parent)
        Lays out the specified container using this layout.

        This method reshapes the components in the specified target container in order to satisfy the constraints of the RelativeLayout object.

        The grid layout manager determines the size of individual components by dividing the free space in the container into equal-sized portions according to the number of rows and columns in the layout. The container's free space equals the container's size minus any insets and any specified horizontal or vertical gap. All components in a grid layout are given the same size.

        Specified by:
        layoutContainer in interface java.awt.LayoutManager
        Parameters:
        target - the container in which to do the layout
        See Also:
        Container, Container.doLayout()
      • adjustForRounding

        protected void adjustForRounding​(int[] relativeSpace,
                                         int spaceRemaining)
      • maximumLayoutSize

        public java.awt.Dimension maximumLayoutSize​(java.awt.Container target)
        There is no maximum.
        Specified by:
        maximumLayoutSize in interface java.awt.LayoutManager2
      • getLayoutAlignmentX

        public float getLayoutAlignmentX​(java.awt.Container parent)
        Returns the alignment along the x axis. Use center alignment.
        Specified by:
        getLayoutAlignmentX in interface java.awt.LayoutManager2
      • getLayoutAlignmentY

        public float getLayoutAlignmentY​(java.awt.Container parent)
        Returns the alignment along the y axis. Use center alignment.
        Specified by:
        getLayoutAlignmentY in interface java.awt.LayoutManager2
      • invalidateLayout

        public void invalidateLayout​(java.awt.Container target)
        Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
        Specified by:
        invalidateLayout in interface java.awt.LayoutManager2
      • toString

        public java.lang.String toString()
        Returns the string representation of this column layout's values.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this grid layout