Table Layout implements LayoutManager2 : 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 




Table Layout implements LayoutManager2
    

/* Copyright (c) 2006, 2009, Carl Burch. License information is located in the
 * com.cburch.logisim.Main source code and at www.cburch.com/logisim/. */
 

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager2;
import java.util.ArrayList;

public class TableLayout implements LayoutManager2 {
    private int colCount;
    private ArrayList contents; // of Component[]
    private int curRow;
    private int curCol;
    private Dimension prefs;
    private int[] prefRow;
    private int[] prefCol;
    private double[] rowWeight;
    
    public TableLayout(int colCount) {
        this.colCount = colCount;
        this.contents = new ArrayList();
        this.curRow = 0;
        this.curCol = 0;
    }
    
    public void setRowWeight(int rowIndex, double weight) {
        if(weight < 0) {
            throw new IllegalArgumentException("weight must be nonnegative");
        }
        if(rowIndex < 0) {
            throw new IllegalArgumentException("row index must be nonnegative");
        }
        if((rowWeight == null || rowIndex >= rowWeight.length&& weight != 0.0) {
            double[] a = new double[rowIndex + 10];
            if(rowWeight != nullSystem.arraycopy(rowWeight, 0, a, 0, rowWeight.length);
            rowWeight = a;
        }
        rowWeight[rowIndex= weight;
    }

    public void addLayoutComponent(String name, Component comp) {
        while(curRow >= contents.size()) {
            contents.add(new Component[colCount]);
        }
        Component[] rowContents = (Component[]) contents.get(curRow);
        rowContents[curCol= comp;
        ++curCol;
        if(curCol == colCount) {
            ++curRow;
            curCol = 0;
        }
        prefs = null;
    }

    public void addLayoutComponent(Component comp, Object constraints) {
        if(constraints instanceof TableConstraints) {
            TableConstraints con = (TableConstraintsconstraints;
            if(con.getRow() >= 0curRow = con.getRow();
            if(con.getCol() >= 0curCol = con.getCol();
        }
        addLayoutComponent("", comp);
    }

    public void removeLayoutComponent(Component comp) {
        for(int i = 0, n = contents.size(); i < n; i++) {
            Component[] row = (Component[]) contents.get(i);
            for(int j = 0; j < row.length; j++) {
                if(row[j== comp) {
                    row[jnull;
                    return;
                }
            }
        }
        prefs = null;
    }

    public Dimension preferredLayoutSize(Container parent) {
        if(prefs == null) {
            int[] prefCol = new int[colCount];
            int[] prefRow = new int[contents.size()]
            int height = 0;
            for(int i = 0; i < prefRow.length; i++) {
                Component[] row = (Component[]) contents.get(i);
                int rowHeight = 0;
                for(int j = 0; j < row.length; j++) {
                    if(row[j!= null) {
                        Dimension dim = row[j].getPreferredSize();
                        if(dim.height > rowHeightrowHeight = dim.height;
                        if(dim.width > prefCol[j]) prefCol[j= dim.width;
                    }
                }
                prefRow[i= rowHeight;
                height += rowHeight;
            }
            int width = 0;
            for(int i = 0; i < prefCol.length; i++) {
                width += prefCol[i];
            }
            this.prefs = new Dimension(width, height);
            this.prefRow = prefRow;
            this.prefCol = prefCol;
        }
        return new Dimension(prefs);
    }

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

    public Dimension maximumLayoutSize(Container parent) {
        return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
    }

    public float getLayoutAlignmentX(Container parent) {
        return 0.5f;
    }

    public float getLayoutAlignmentY(Container parent) {
        return 0.5f;
    }

    public void layoutContainer(Container parent) {
        Dimension pref = preferredLayoutSize(parent);
        int[] prefRow = this.prefRow;
        int[] prefCol = this.prefCol;
        Dimension size = parent.getSize();
        
        double y0;
        int yRemaining = size.height - pref.height;
        double rowWeightTotal = 0.0;
        if(yRemaining != && rowWeight != null) {
            for(int i = 0; i < rowWeight.length; i++) {
                rowWeightTotal += rowWeight[i];
            }
        }
        if(rowWeightTotal == 0.0 && yRemaining > 0) {
            y0 = yRemaining / 2.0;
        else {
            y0 = 0;
        }
        
        int x0 = (size.width - pref.width2;
        if(x0 < 0x0 = 0;
        double y = y0;
        for(int i = 0, n = contents.size(); i < n; i++) {
            Component[] row = (Component[]) contents.get(i);
            int yRound = (int) (y + 0.5);
            int x = x0;
            for(int j = 0; j < row.length; j++) {
                Component comp = row[j];
                if(comp != null) {
                    row[j].setBounds(x, yRound, prefCol[j], prefRow[i]);
                }
                x += prefCol[j];
            }
            y += prefRow[i];
            if(rowWeightTotal > && i < rowWeight.length) {
                y += yRemaining * rowWeight[i/ rowWeightTotal;
            }
        }
        
        
        // TODO Auto-generated method stub
        
    }

    public void invalidateLayout(Container parent) {
        prefs = null;
    }

}
/* Copyright (c) 2006, 2009, Carl Burch. License information is located in the
 * com.cburch.logisim.Main source code and at www.cburch.com/logisim/. */
 class TableConstraints {
    public static TableConstraints at(int row, int col) {
        return new TableConstraints(row, col);
    }
    
    private int col;
    private int row;
    
    private TableConstraints(int row, int col) {
        this.col = col;
        this.row = row;
    }
    
    int getRow() {
        return row;
    }
    
    int getCol() {
        return col;
    }
}

   
    
    
    
  














Related examples in the same category
1.Custom layout: EdgeLayout
2.Customized layout managerCustomized layout manager
3.ColumnLayoutColumnLayout
4.Applet GUI demo of TreeLayout layout manager
5.Relative Layout Manager for Java J2SE
6.Basically two (or more) columns of different, but constant, widths
7.GraphPaperLayoutGraphPaperLayout
8.Table Layout
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.