Customized layout manager : Customized Layout « Swing JFC « Java






Customized layout manager

Customized layout manager
    
import java.awt.Button;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class CircleLayoutTest extends JFrame {
  public CircleLayoutTest() {
    setTitle("CircleLayoutTest");
    setSize(300, 300);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    getContentPane().setLayout(new CircleLayout());
    getContentPane().add(new Button("3"));
    getContentPane().add(new Button("4"));
    getContentPane().add(new Button("5"));
    getContentPane().add(new Button("6"));
    getContentPane().add(new Button("7"));
    getContentPane().add(new Button("8"));
    getContentPane().add(new Button("9"));
    getContentPane().add(new Button("10"));
    getContentPane().add(new Button("11"));
    getContentPane().add(new Button("12"));
    getContentPane().add(new Button("1"));
    getContentPane().add(new Button("2"));
  }

  public static void main(String[] args) {
    JFrame f = new CircleLayoutTest();
    f.show();
  }

}

class CircleLayout implements LayoutManager {
  private int minWidth = 0;

  private int minHeight = 0;

  private int preferredWidth = 0, preferredHeight = 0;

  private boolean sizesSet = false;

  private int maxComponentWidth = 0;

  private int maxComponentHeight = 0;

  public void addLayoutComponent(String name, Component comp) {
  }

  public void removeLayoutComponent(Component comp) {
  }

  public void setSizes(Container parent) {
    if (sizesSet)
      return;
    int comCount = parent.getComponentCount();

    for (int i = 0; i < comCount; i++) {
      Component c = parent.getComponent(i);
      if (c.isVisible()) {
        Dimension size = c.getPreferredSize();
        maxComponentWidth = Math.max(maxComponentWidth, size.width);
        maxComponentHeight = Math.max(maxComponentWidth, size.height);
        preferredHeight += size.height;
      }
    }
    preferredHeight += maxComponentHeight;
    preferredWidth = 2 * maxComponentWidth;
    minHeight = preferredHeight;
    minWidth = preferredWidth;
    sizesSet = true;
  }

  public Dimension preferredLayoutSize(Container parent) {
    Dimension dim = new Dimension(0, 0);
    setSizes(parent);
    Insets insets = parent.getInsets();
    dim.width = preferredWidth + insets.left + insets.right;
    dim.height = preferredHeight + insets.top + insets.bottom;
    return dim;
  }

  public Dimension minimumLayoutSize(Container parent) {
    return preferredLayoutSize(parent);
  }

  public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    int containerWidth = parent.getSize().width - insets.left
        - insets.right;
    int containerHeight = parent.getSize().height - insets.top
        - insets.bottom;
    int xradius = (containerWidth - maxComponentWidth) / 2;
    int yradius = (containerHeight - maxComponentHeight) / 2;

    setSizes(parent);
    int centerX = insets.left + containerWidth / 2;
    int centerY = insets.top + containerHeight / 2;

    int comCount = parent.getComponentCount();
    for (int i = 0; i < comCount; i++) {
      Component c = parent.getComponent(i);
      if (c.isVisible()) {
        Dimension size = c.getPreferredSize();
        double angle = 2 * Math.PI * i / comCount;
        int x = centerX + (int) (Math.cos(angle) * xradius);
        int y = centerY + (int) (Math.sin(angle) * yradius);

        c.setBounds(x - size.width / 2, y - size.height / 2, size.width,
            size.height);
      }
    }
  }
}
           
         
    
    
    
  








Related examples in the same category

1.Custom layout: EdgeLayout
2.ColumnLayoutColumnLayout
3.Applet GUI demo of TreeLayout layout manager
4.Relative Layout Manager for Java J2SE
5.Basically two (or more) columns of different, but constant, widths
6.GraphPaperLayoutGraphPaperLayout
7.Table Layout
8.Table Layout implements LayoutManager2
9.Table layout manager
10.Flex Layout
11.Square Layout
12.Center Layout
13.Wrapper Layout
14.Tile Layout
15.Custom Layout DemoCustom Layout Demo
16.X Y Layout
17.DividerLayout is layout that divides two components with the column of actions
18.Stack Layout, uses an orientation to determine if the contents should be arranged horizontally or vertically.
19.A simple layoutmanager to overlay all components of a parent.
20.A layout manager that displays a single component in the center of its container.
21.A layout manager that spaces components over six columns in seven different formats.
22.Compents are laid out in a circle.
23.Special simple layout used in TabbedContainer
24.Place components at exact locations (x, y, width, height) and then determine how they behave when the window containing them (their parent) is resized
25.Specialised layout manager for a grid of components.