A vertical flow layout is similar to a flow layuot but it layouts the components vertically instead of horizontally. : Layout « Swing JFC « Java






A vertical flow layout is similar to a flow layuot but it layouts the components vertically instead of horizontally.

        
//----------------------------------------------------
// Image Mover
// (C) Vassili Dzuba, 2000
// distributed under the Artistic License
//----------------------------------------------------

//package modrcon;

import java.awt.LayoutManager;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.Insets;

/**
 *  A vertical flow layout is similar to a flow layuot but it layouts the
 *  components vertically instead of horizontally.
 *
 * @author     Vassili Dzuba
 * @since      March 24, 2001
 */
public class VerticalFlowLayout implements LayoutManager, java.io.Serializable {

  int _halign;
  int _valign;
  int _hgap;
  int _vgap;

  /**
   *  Description of the Field
   */
  public final static int TOP = 0;
  /**
   *  Description of the Field
   */
  public final static int CENTER = 1;
  /**
   *  Description of the Field
   */
  public final static int BOTTOM = 2;
  /**
   *  Description of the Field
   */
  public final static int LEFT = 3;
  /**
   *  Description of the Field
   */
  public final static int RIGHT = 4;


  /**
   *  Constructor for the VerticalFlowLayout object
   */
  public VerticalFlowLayout() {
     this(CENTER, CENTER, 5, 5);
  }


  /**
   *  Constructor for the VerticalFlowLayout object
   *
   *@param  halign  Description of Parameter
   *@param  valign  Description of Parameter
   */
  public VerticalFlowLayout(int halign, int valign) {
    this(halign, valign, 5, 5);
  }


  /**
   *  Constructor for the VerticalFlowLayout object
   *
   *@param  halign  Description of Parameter
   *@param  valign  Description of Parameter
   *@param  hgap    Description of Parameter
   *@param  vgap    Description of Parameter
   */
  public VerticalFlowLayout(int halign, int valign, int hgap, int vgap) {
    _hgap = hgap;
    _vgap = vgap;
    setAlignment(halign, valign);
  }


  /**
   *  Sets the Alignment attribute of the VerticalFlowLayout object
   *
   *@param  halign  The new Alignment value
   *@param  valign  The new Alignment value
   */
  public void setAlignment(int halign, int valign) {
    _halign = halign;
    _valign = valign;
  }


  /**
   *  Sets the Hgap attribute of the VerticalFlowLayout object
   *
   *@param  hgap  The new Hgap value
   */
  public void setHgap(int hgap) {
    _hgap = hgap;
  }


  /**
   *  Sets the Vgap attribute of the VerticalFlowLayout object
   *
   *@param  vgap  The new Vgap value
   */
  public void setVgap(int vgap) {
    _vgap = vgap;
  }


  /**
   *  Gets the Halignment attribute of the VerticalFlowLayout object
   *
   *@return    The Halignment value
   */
  public int getHalignment() {
    return _halign;
  }


  /**
   *  Gets the Valignment attribute of the VerticalFlowLayout object
   *
   *@return    The Valignment value
   */
  public int getValignment() {
    return _valign;
  }


  /**
   *  Gets the Hgap attribute of the VerticalFlowLayout object
   *
   *@return    The Hgap value
   */
  public int getHgap() {
    return _hgap;
  }


  /**
   *  Gets the Vgap attribute of the VerticalFlowLayout object
   *
   *@return    The Vgap value
   */
  public int getVgap() {
    return _vgap;
  }


  /**
   *  Adds a feature to the LayoutComponent attribute of the VerticalFlowLayout
   *  object
   *
   *@param  name  The feature to be added to the LayoutComponent attribute
   *@param  comp  The feature to be added to the LayoutComponent attribute
   */
  public void addLayoutComponent(String name, Component comp) {
  }


  /**
   *  Description of the Method
   *
   *@param  comp  Description of Parameter
   */
  public void removeLayoutComponent(Component comp) {
  }


  /**
   *  Description of the Method
   *
   *@param  target  Description of Parameter
   *@return         Description of the Returned Value
   */
  public Dimension preferredLayoutSize(Container target) {
    synchronized (target.getTreeLock()) {
      Dimension dim = new Dimension(0, 0);
      int nmembers = target.getComponentCount();
      boolean firstVisibleComponent = true;

      for (int ii = 0; ii < nmembers; ii++) {
        Component m = target.getComponent(ii);
        if (m.isVisible()) {
          Dimension d = m.getPreferredSize();
          dim.width = Math.max(dim.width, d.width);
          if (firstVisibleComponent) {
            firstVisibleComponent = false;
          }
          else {
            dim.height += _vgap;
          }
          dim.height += d.height;
        }
      }
      Insets insets = target.getInsets();
      dim.width += insets.left + insets.right + _hgap * 2;
      dim.height += insets.top + insets.bottom + _vgap * 2;
      return dim;
    }
  }


  /**
   *  Description of the Method
   *
   *@param  target  Description of Parameter
   *@return         Description of the Returned Value
   */
  public Dimension minimumLayoutSize(Container target) {
    synchronized (target.getTreeLock()) {
      Dimension dim = new Dimension(0, 0);
      int nmembers = target.getComponentCount();
      boolean firstVisibleComponent = true;

      for (int ii = 0; ii < nmembers; ii++) {
        Component m = target.getComponent(ii);
        if (m.isVisible()) {
          Dimension d = m.getPreferredSize();
          dim.width = Math.max(dim.width, d.width);
          if (firstVisibleComponent) {
            firstVisibleComponent = false;
          }
          else {
            dim.height += _vgap;
          }
          dim.height += d.height;
        }
      }
      Insets insets = target.getInsets();
      dim.width += insets.left + insets.right + _hgap * 2;
      dim.height += insets.top + insets.bottom + _vgap * 2;
      return dim;
    }
  }


  /**
   *  Description of the Method
   *
   *@param  target  Description of Parameter
   */
  public void layoutContainer(Container target) {
    synchronized (target.getTreeLock()) {
      Insets insets = target.getInsets();
      int maxheight = target.getHeight() - (insets.top + insets.bottom + _vgap * 2);
      int nmembers = target.getComponentCount();
      int y = 0;

      Dimension preferredSize = preferredLayoutSize(target);
      Dimension targetSize = target.getSize();

      switch (_valign) {
        case TOP:
          y = insets.top;
          break;
        case CENTER:
          y = (targetSize.height - preferredSize.height) / 2;
          break;
        case BOTTOM:
          y = targetSize.height - preferredSize.height - insets.bottom;
          break;
      }

      for (int i = 0; i < nmembers; i++) {
        Component m = target.getComponent(i);
        if (m.isVisible()) {
          Dimension d = m.getPreferredSize();
          m.setSize(d.width, d.height);

          if ((y + d.height) <= maxheight) {
            if (y > 0) {
              y += _vgap;
            }

            int x = 0;
            switch (_halign) {
              case LEFT:
                x = insets.left;
                break;
              case CENTER:
                x = (targetSize.width - d.width) / 2;
                break;
              case RIGHT:
                x = targetSize.width - d.width - insets.right;
                break;
            }

            m.setLocation(x, y);

            y += d.getHeight();

          }
          else {
            break;
          }
        }
      }
    }
  }


  /** {@inheritDoc} */
  @Override public String toString() {
    String halign = "";
    switch (_halign) {
      case TOP:
        halign = "top";
        break;
      case CENTER:
        halign = "center";
        break;
      case BOTTOM:
        halign = "bottom";
        break;
    }
    String valign = "";
    switch (_valign) {
      case TOP:
        valign = "top";
        break;
      case CENTER:
        valign = "center";
        break;
      case BOTTOM:
        valign = "bottom";
        break;
    }
    return getClass().getName() + "[hgap=" + _hgap + ",vgap=" + _vgap + ",halign=" + halign + ",valign=" + valign + "]";
  }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Laying Out Components in a Flow (Left-to-Right, Top-to-Bottom)
2.Use FlowLayout to hold checkBox, Label and TextField
3.Vertical and horizontal BoxLayoutsVertical and horizontal BoxLayouts
4.Box Layout: Adding struts.Box Layout: Adding struts.
5.Using GlueUsing Glue
6.Rigid areas are like pairs of strutsRigid areas are like pairs of struts
7.Demonstrates BorderLayoutDemonstrates BorderLayout
8.Demonstrates GridLayoutDemonstrates GridLayout
9.Demonstrates FlowLayoutDemonstrates FlowLayout
10.A test of the BoxLayout manager using the Box utility classA test of the BoxLayout manager using the Box utility class
11.A test of the box layout manager using the Box utility class 2A test of the box layout manager using the Box utility class 2
12.A test of the OverlayLayout manager allowing experimentationA test of the OverlayLayout manager allowing experimentation
13.Box layout manager using the Box utility classBox layout manager using the Box utility class
14.BoxLayout demo 1BoxLayout demo 1
15.A demonstration of the SpringLayout class
16.A test of the BoxLayout manager using the Box utility class 3A test of the BoxLayout manager using the Box utility class 3
17.BoxLayout AlignmentBoxLayout Alignment
18.BoxLayout alignment 2BoxLayout alignment 2
19.BoxLayout Component alignmentBoxLayout Component alignment
20.BoxLayout X Y alignmentBoxLayout X Y alignment
21.BoxLayout: Glue SampleBoxLayout: Glue Sample
22.Layout: Overlay SampleLayout: Overlay Sample
23.BoxLayout: setAlignmentX setAlignmentYBoxLayout: setAlignmentX setAlignmentY
24.Component AlignmentComponent Alignment
25.GridLayout Demo 3GridLayout Demo 3
26.BoxLayout SampleBoxLayout Sample
27.BoxLayout Demo 3BoxLayout Demo 3
28.Various layoutsVarious layouts
29.BoxLayout Demo 4BoxLayout Demo 4
30.GridLayout DemoGridLayout Demo
31.CardLayout DemoCardLayout Demo
32.Using CardLayout
33.Laying Out a Screen with CardLayoutLaying Out a Screen with CardLayout
34.Laying out a screen with SpringLayoutLaying out a screen with SpringLayout
35.BorderLayout PaneBorderLayout Pane
36.Add buttons to all parts of a BorderLayout
37.A BorderLayout divides the space into five regions: North, West, South, East and Centre.
38.A typical usage of a border layout manager.
39.BoxLayout PaneBoxLayout Pane
40.FlowLayout PaneFlowLayout Pane
41.GridLayout PaneGridLayout Pane
42.Laying Out Components in a Grid
43.NullLayout PaneNullLayout Pane
44.Without layout manager, we position components using absolute values.
45.Simpler CardLayout demo
46.Use SpringLayout to create a single row of componentsUse SpringLayout to create a single row of components
47. Spring Demo 1 Spring Demo 1
48.Spring Demo 2Spring Demo 2
49.Spring Demo 3Spring Demo 3
50.Spring Demo 4Spring Demo 4
51.Spring Compact GridSpring Compact Grid
52.Spring Form Spring Form
53.SpringLayout Utilities
54.Absolute Layout DemoAbsolute Layout Demo
55.A horizontal layout layout manager that allocates horizontal space in specified proportions
56.Laying Out Components Using Absolute Coordinates
57.A vertical layout manager similar to java.awt.FlowLayout
58.A JTextField for displaying insets.
59.A layout manager that lays out components along a circleA layout manager that lays out components along a circle
60.A frame that uses a group layout to arrange font selection components
61.Utility methods for creating form- or grid-style layouts with SpringLayout.