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. 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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 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. 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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.