Customized layout manager : Customized Layout « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » Customized LayoutScreenshots 
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. Custom Layout DemoCustom Layout Demo
ww__w__.___j_a__v_a_2_s__.___c_om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.