Custom layout: EdgeLayout : 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 
Custom layout: EdgeLayout

/*
Code from Desktop Java Live Source

URL: http://www.sourcebeat.com/downloads/

*/


import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class EdgeLayoutExample {
    public static JPanel createPanel() {
        JPanel outerPanel = new JPanel();
        outerPanel.setLayout(new EdgeLayout());
        outerPanel.add(new JButton("West1"), EdgeLayout.WEST);
        outerPanel.add(new JButton("North1"), EdgeLayout.NORTH);
        outerPanel.add(new JButton("West2"), EdgeLayout.WEST);
        outerPanel.add(new JButton("North2"), EdgeLayout.NORTH);
        outerPanel.add(new JButton("East1"), EdgeLayout.EAST);
        outerPanel.add(new JButton("South1"), EdgeLayout.SOUTH);
        outerPanel.add(new JButton("West3"), EdgeLayout.WEST);
        outerPanel.add(new JButton("West4"), EdgeLayout.WEST);
        outerPanel.add(new JButton("South2"), EdgeLayout.SOUTH);
        outerPanel.add(new JButton("South3"), EdgeLayout.SOUTH);
        outerPanel.add(new JButton("Center1"), EdgeLayout.CENTER);
        return outerPanel;
    }
    public static void main(String[] a){
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(1);
      f.add(createPanel());
      f.pack();
      f.setVisible(true);
    
}


class EdgeLayout implements LayoutManager2, java.io.Serializable {
    private List components = new ArrayList();
    private HashMap constraints = new HashMap();

    public static final String CENTER = "center";
    public static final String NORTH = "north";
    public static final String SOUTH = "south";
    public static final String EAST = "east";
    public static final String WEST = "west";

    public void addLayoutComponent(Component comp, Object constraints) {
        synchronized (comp.getTreeLock()) {
            if (constraints instanceof String && comp != null) {
                this.components.add(comp);
                this.constraints.put(comp, constraints);
            else {
                throw new IllegalArgumentException("Invalid component constraints.");
            }
        }
    }

    public Dimension maximumLayoutSize(Container target) {
        //Return a very large size since this layout manager will fill all available space.
        return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
    }

    public float getLayoutAlignmentX(Container target) {
        //Centered on the X
        return (float0.5;
    }

    public float getLayoutAlignmentY(Container target) {
        //Centered on the Y
        return (float0.5;
    }

    public void invalidateLayout(Container target) {
        //There is no caching in EdgeLayout to there is nothing to invalidate
    }

    public void addLayoutComponent(String name, Component comp) {
        throw new IllegalArgumentException("EdgeLayout only supports addition with contraints.");
    }

    public void removeLayoutComponent(Component comp) {
        synchronized (comp.getTreeLock()) {
            this.components.remove(comp);
            this.constraints.remove(comp);
        }
    }

    public Dimension preferredLayoutSize(Container parent) {
        synchronized (parent.getTreeLock()) {
            int width = 0;
            int height = 0;

            //Add the preferred widths of all EAST/WEST components
            //Add the preferred height of all NORTH/SOUTH components
            for (int i = 0; i < this.components.size(); i++) {
                Component c = (Componentthis.components.get(i);
                if (this.constraints.get(c).equals(WEST|| this.constraints.get(c).equals(EAST)) {
                    width += c.getPreferredSize().getWidth();
                else {
                    height += c.getPreferredSize().getHeight();
                }
            }

            width += parent.getInsets().right + parent.getInsets().left;
            height += parent.getInsets().top + parent.getInsets().bottom;

            return new Dimension(width, height);
        }
    }

    public Dimension minimumLayoutSize(Container parent) {
        synchronized (parent.getTreeLock()) {
            int width = 0;
            int height = 0;

            //Add the minimum  widths of all EAST/WEST components
            //Add the minimum height of all NORTH/SOUTH components
            for (int i = 0; i < this.components.size(); i++) {
                Component c = (Componentthis.components.get(i);
                if (this.constraints.get(c).equals(WEST|| this.constraints.get(c).equals(EAST)) {
                    width += c.getMinimumSize().getWidth();
                else {
                    height += c.getMinimumSize().getHeight();
                }
            }

            width += parent.getInsets().right + parent.getInsets().left;
            height += parent.getInsets().top + parent.getInsets().bottom;

            return new Dimension(width, height);
        }
    }

    public void layoutContainer(Container parent) {
        synchronized (parent.getTreeLock()) {
            Insets insets = parent.getInsets();
            int top = insets.top;
            int left = insets.left;

            Dimension minimumSize = minimumLayoutSize(parent);

            int height = minimumSize.height;
            int width = minimumSize.width;

            int availableHeight = parent.getHeight() - insets.bottom - insets.top;
            int availableWidth = parent.getWidth() - insets.left - insets.right;
            if (height < availableHeight) {
                height = availableHeight;
            }
            if (width < availableWidth) {
                width = availableWidth;
            }

            int bottom = availableHeight;
            int right = availableWidth;

            Dimension preferredSize = preferredLayoutSize(parent);

            int preferredWidthAvailable = width - preferredSize.width;
            int preferredHeightAvailable = height - preferredSize.height;


            Component centerComp = null;

            for (int i = 0; i < this.components.size(); i++) {
                Component c = (Componentthis.components.get(i);
                String constraint = (Stringthis.constraints.get(c);

                if (constraint.equals(CENTER)) {
                    centerComp = c;
                else {
                    int compHeight;
                    int compWidth;
                    int xOrigin;
                    int yOrigin;


                    if (constraint.equals(NORTH|| constraint.equals(SOUTH)) {
                        compWidth = width;

                        if (preferredHeightAvailable > 0) {
                            int preferredHeightNeeded = c.getPreferredSize().height - c.getMinimumSize().height;
                            if (preferredHeightAvailable > preferredHeightNeeded) {
                                compHeight = c.getPreferredSize().height;
                                preferredHeightAvailable -= preferredHeightNeeded;
                            else {
                                compHeight = c.getMinimumSize().height + preferredHeightAvailable;
                                preferredHeightAvailable = 0;
                            }
                        else {
                            compHeight = c.getMinimumSize().height;
                        }
                        height = height - compHeight;

                        xOrigin = left;

                        if (constraint.equals(NORTH)) {
                            yOrigin = top;
                            top += compHeight;
                        else {
                            yOrigin = bottom - compHeight;
                            bottom = yOrigin;
                        }
                    else {
                        compHeight = height;
                        if (preferredWidthAvailable > 0) {
                            int preferredWidthNeeded = c.getPreferredSize().width - c.getMinimumSize().width;
                            if (preferredWidthAvailable > preferredWidthNeeded) {
                                compWidth = c.getPreferredSize().width;
                                preferredWidthAvailable -= preferredWidthNeeded;
                            else {
                                compWidth = c.getMinimumSize().width + preferredWidthAvailable;
                                preferredWidthAvailable = 0;
                            }
                        else {
                            compWidth = c.getMinimumSize().width;
                        }
                        width = width - compWidth;

                        yOrigin = top;

                        if (constraint.equals(WEST)) {
                            xOrigin = left;
                            left += compWidth;
                        else {
                            xOrigin = right - compWidth;
                            right = xOrigin;
                        }
                    }
                    c.setSize(compWidth, compHeight);
                    c.setBounds(xOrigin, yOrigin, compWidth, compHeight);
                }
                if (centerComp != null) {
                    c.setSize(width, height);
                    c.setBounds(left, top, width, height);
                }
            }
        }
    }
}

           
       
Related examples in the same category
1. Customized layout managerCustomized layout manager
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__va__2_s_.__c___o_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.