public class RowColumnLayout extends Object implements LayoutManager
class RowColumnExample extends Panel {
protected RowColumnLayout row_col_layout;
protected int rows[] = {2, 2};
//Resize button class
class ResizeButton extends Canvas {
ResizeButton()
{
setBackground(Color.lightGray);
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
public void paint(Graphics g)
{
Rectangle d = getBounds();
g.draw3DRect(0, 0, d.width-1, d.height-1, true);
g.dispose();
}
public void print(Graphics g){}
public void printAll(Graphics g){}
}
public RowColumnExample()
{
row_col_layout = new RowColumnLayout(rows);
setLayout(row_col_layout);
//The number of component to add to panel are 4 (2 column each colum 2 component)
//Before add component we must add num_component - 1 canvas objet that are used
//to interact with layout manager for interactive resize of width and height of
//the grid
ResizeButton b;
for(int i = 0; i < 4 - 1; i++) {
add(b = new ResizeButton());
b.addMouseListener(new MouseAdapter()
{
//Action used to resize column width or component height
public void mouseReleased(MouseEvent e)
{
Component ob = e.getComponent();
if(ob instanceof ResizeButton)
row_col_layout.ResizeRowColumn(ob, e.getPoint().x, e.getPoint().y);
}
});
b.addMouseMotionListener(new MouseMotionAdapter()
{
//Action used to draw resiaze line on the component, only if component impement double buffering
public void mouseDragged(MouseEvent e)
{
Component ob = e.getComponent();
row_col_layout.DrawResize(ob, e.getPoint().x, e.getPoint().y);
}
});
}
for(int i = 0, k = 0; i < columns; i++)
{
for(int j = 0; j < rows[i]; j++)
{
add();
k++;
}
}
validate();
}
}
RowColumnContainer| Constructor and Description |
|---|
RowColumnLayout(int[] row)
Costruct a RowColumnLayout with a column number defined by _column argument
and number of object per culomn defined by row[] vector
|
RowColumnLayout(int[] row,
float[] ph,
float[] pw)
Costruct a RowColumnLayout with a column number defined by _column argument
and number of object per column defined by row[] vector.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addLayoutComponent(String name,
Component comp)
Do nothing Required by LayoutManager.
|
void |
DrawResize(Component _b,
int x,
int y)
Method to draw orizontal or vertical line during resize componet or column.
|
int |
GetColumns()
Return number of column
|
float[] |
getPercentHeight() |
float |
getPercentHeight(int i)
Return normalize height of ith componet
|
float[] |
getPercentWidth() |
float |
getPercentWidth(int i)
Return width of ith column
|
int[] |
GetRows()
Return integer vector of number of component in each column
|
void |
layoutContainer(Container parent)
Required by LayoutManager.
|
Dimension |
minimumLayoutSize(Container parent)
Required by LayoutManager
|
Dimension |
preferredLayoutSize(Container parent)
Required by LayoutManager
|
void |
removeLayoutComponent(Component comp)
Do nothing Required by LayoutManager.
|
void |
ResizeRowColumn(Component _b)
Calculate equal space of column and equal width of component in column
|
void |
ResizeRowColumn(Component _b,
int x,
int y)
Culomn width and component height resize on x or y value.
|
void |
SetPanelSize(float[] ph,
float[] pw)
Set normalize size of column width and component height
|
void |
SetRowColumn(int[] row)
Set number of column an number of component for column
|
void |
SetRowColumn(int[] row,
float[] ph,
float[] pw)
Method used to update number of column end number of componet for column
|
String |
toString()
Returns a string representation of this RowColumnLayout and its values.
|
public RowColumnLayout(int[] row)
column - Number of columnrow - row[i] define number of component in column ipublic RowColumnLayout(int[] row,
float[] ph,
float[] pw)
row - row[i] define number of component in column iph - Vector of normalize height of component. The sum of ph[x] of the
objects in a column must be 1.pw - Vector of normalize width of the culomn. The sum of pw[x] must be
1public void addLayoutComponent(String name, Component comp)
addLayoutComponent in interface LayoutManagername - Component namecomp - Componentpublic void DrawResize(Component _b, int x, int y)
_b - Canvas object used to interact with mouse dragx - Canvas x positiony - Canvas y positionRowColumnComponetpublic int GetColumns()
public float[] getPercentHeight()
public float getPercentHeight(int i)
i - index of componentpublic float[] getPercentWidth()
public float getPercentWidth(int i)
i - index of columnpublic int[] GetRows()
public void layoutContainer(Container parent)
layoutContainer in interface LayoutManagerparent - component containerpublic Dimension minimumLayoutSize(Container parent)
minimumLayoutSize in interface LayoutManagerparent - component containerpublic Dimension preferredLayoutSize(Container parent)
preferredLayoutSize in interface LayoutManagerparent - component containerpublic void removeLayoutComponent(Component comp)
removeLayoutComponent in interface LayoutManagercomp - componentpublic void ResizeRowColumn(Component _b)
_b - Canvas object used to interact with mouse if _b is column resize
canvas equal width culomn are performed if _b is componet column
resize all component in column are resized to equal height.public void ResizeRowColumn(Component _b, int x, int y)
_b - Canvas object used to interact with mouse if _b is column resize
canvas columns width are resize on x value if _b is componet column
resize component height are resize on y value.x - x positiony - y positionpublic void SetPanelSize(float[] ph,
float[] pw)
ph - Vector of normalize height of component. The sum of ph[x] for objet
in a column must be 1.pw - Vector of normalize width of the culomn. Sum of pw[x] must be 1public void SetRowColumn(int[] row)
row - row[i] define number of component in column ipublic void SetRowColumn(int[] row,
float[] ph,
float[] pw)
row - row[i] define number of component in column iph - Vector of normalize height of component. The sum of ph[x] for
objet in a column must be 1.pw - Vector of normalize width of the culomn. Sum of pw[x] must be 1Copyright © 2022. All rights reserved.