Customized layout manager : Customized Layout « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » Customized Layout 




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(300300);
    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 = * maxComponentWidth;
    minHeight = preferredHeight;
    minWidth = preferredWidth;
    sizesSet = true;
  }

  public Dimension preferredLayoutSize(Container parent) {
    Dimension dim = new Dimension(00);
    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 - maxComponentWidth2;
    int yradius = (containerHeight - maxComponentHeight2;

    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 = * 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.
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.