X Y Layout : Custom Layout « Swing « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Swing » Custom Layout 




//Revised from enhydra swing

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager2;
import java.awt.Rectangle;
import java.io.Serializable;
import java.util.Hashtable;

public class XYLayout implements LayoutManager2, Serializable {

  private static final long serialVersionUID = 200L;

  int width;

  int height;

  Hashtable info;

  static final XYConstraints defaultConstraints = new XYConstraints();

  public XYLayout() {
    info = new Hashtable();
  }

  public XYLayout(int width, int height) {
    info = new Hashtable();
    this.width = width;
    this.height = height;
  }

  public int getWidth() {
    return width;
  }

  public void setWidth(int width) {
    this.width = width;
  }

  public int getHeight() {
    return height;
  }

  public void setHeight(int height) {
    this.height = height;
  }

  public String toString() {
    return String.valueOf(String.valueOf((new StringBuffer("XYLayout[width=")).append(width)
        .append(",height=").append(height).append("]")));
  }

  public void addLayoutComponent(String s, Component component1) {
  }

  public void removeLayoutComponent(Component component) {
    info.remove(component);
  }

  public Dimension preferredLayoutSize(Container target) {
    return getLayoutSize(target, true);
  }

  public Dimension minimumLayoutSize(Container target) {
    return getLayoutSize(target, false);
  }

  public void layoutContainer(Container target) {
    Insets insets = target.getInsets();
    int count = target.getComponentCount();
    for (int i = 0; i < count; i++) {
      Component component = target.getComponent(i);
      if (component.isVisible()) {
        Rectangle r = getComponentBounds(component, true);
        component.setBounds(insets.left + r.x, insets.top + r.y, r.width, r.height);
      }
    }

  }

  public void addLayoutComponent(Component component, Object constraints) {
    if (constraints instanceof XYConstraints)
      info.put(component, constraints);
  }

  public Dimension maximumLayoutSize(Container target) {
    return new Dimension(0x7fffffff0x7fffffff);
  }

  public float getLayoutAlignmentX(Container target) {
    return 0.5F;
  }

  public float getLayoutAlignmentY(Container target) {
    return 0.5F;
  }

  public void invalidateLayout(Container container) {
  }

  Rectangle getComponentBounds(Component component, boolean doPreferred) {
    XYConstraints constraints = (XYConstraintsinfo.get(component);
    if (constraints == null)
      constraints = defaultConstraints;
    Rectangle r = new Rectangle(constraints.x, constraints.y, constraints.width, constraints.height);
    if (r.width <= || r.height <= 0) {
      Dimension d = doPreferred ? component.getPreferredSize() : component.getMinimumSize();
      if (r.width <= 0)
        r.width = d.width;
      if (r.height <= 0)
        r.height = d.height;
    }
    return r;
  }

  Dimension getLayoutSize(Container target, boolean doPreferred) {
    Dimension dim = new Dimension(00);
    if (width <= || height <= 0) {
      int count = target.getComponentCount();
      for (int i = 0; i < count; i++) {
        Component component = target.getComponent(i);
        if (component.isVisible()) {
          Rectangle r = getComponentBounds(component, doPreferred);
          dim.width = Math.max(dim.width, r.x + r.width);
          dim.height = Math.max(dim.height, r.y + r.height);
        }
      }

    }
    if (width > 0)
      dim.width = width;
    if (height > 0)
      dim.height = height;
    Insets insets = target.getInsets();
    dim.width += insets.left + insets.right;
    dim.height += insets.top + insets.bottom;
    return dim;
  }

}

class XYConstraints implements Cloneable, Serializable {

  int x;

  int y;

  int width;

  int height;

  public XYConstraints() {
    this(0000);
  }

  public XYConstraints(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }

  public int getX() {
    return x;
  }

  public void setX(int x) {
    this.x = x;
  }

  public int getY() {
    return y;
  }

  public void setY(int y) {
    this.y = y;
  }

  public int getWidth() {
    return width;
  }

  public void setWidth(int width) {
    this.width = width;
  }

  public int getHeight() {
    return height;
  }

  public void setHeight(int height) {
    this.height = height;
  }

  public int hashCode() {
    return x ^ y * 37 ^ width * 43 ^ height * 47;
  }

  public boolean equals(Object that) {
    if (that instanceof XYConstraints) {
      XYConstraints other = (XYConstraintsthat;
      return other.x == x && other.y == y && other.width == width && other.height == height;
    else {
      return false;
    }
  }

  public Object clone() {
    return new XYConstraints(x, y, width, height);
  }

  public String toString() {
    return String.valueOf(String.valueOf((new StringBuffer("XYConstraints[")).append(x).append(",")
        .append(y).append(",").append(width).append(",").append(height).append("]")));
  }
}














14.97.Custom Layout
14.97.1.Custom Layout: DiagonalLayoutCustom Layout: DiagonalLayout
14.97.2.GraphPaperLayout implements LayoutManager2
14.97.3.Circle Layout
14.97.4.This LayoutManager arranges the components into a column. Components are always given their preferred size
14.97.5.Compents are laid out in a circle.
14.97.6.Specialised layout manager for a grid of components.
14.97.7.Stack Layout, uses an orientation to determine if the contents should be arranged horizontally or vertically.
14.97.8.A layout manager that displays a single component in the center of its container.
14.97.9.A simple layoutmanager to overlay all components of a parent.
14.97.10.A layout manager that lays out components along a central axis
14.97.11.Wrapper Layout
14.97.12.Center Layout
14.97.13.DividerLayout is layout that divides two components with the column of actions
14.97.14.X Y Layout
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.