Custom LayoutManager causes OutOfMemory
Hi
We created one custom LayoutManger.It works fine ,but when thw window is resized by dragging the window , system hangs and gives OutOfMemory error afters some time.
Screen contains one Panel on left side and other space hungry Panel or right side. The top level conatiner follows BorderLayout.
Left side Panel uses Custom Layout. If I'm removing this layout everything is perfect. Can anybody advise me where I gone wrong
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
publicclass CustomLayoutManagerimplements LayoutManager{
publicstaticfinalint VERTICAL_GAP = 2;
publicstaticfinalint CELLHEIGHT = 25;
publicstaticint CELLWIDTH = 80;
publicstaticfinalint CELL_STARTX = 0;
privateint vgap = VERTICAL_GAP;
// private int minWidth = 0, minHeight = 0;
// p// preferredWidth = 0, preferredHeight = 0;
publicvoid addLayoutComponent(String arg0, Component arg1){
// TODO Auto-generated method stub
}
publicvoid removeLayoutComponent(Component arg0){
// TODO Auto-generated method stub
}
/* Required by LayoutManager. */
public Dimension preferredLayoutSize(Container parent){
//Always add the container's insets!
synchronized (parent.getTreeLock()){
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int w = 20;
int h = 0;
for (int i = 0; i < ncomponents; i++){
Component comp = parent.getComponent(i);
Dimension d = comp.getPreferredSize();
if (w < d.width){
w = d.width;
}
if (h < d.height){
h = d.height;
}
}
returnnew Dimension(insets.left + insets.right + w, insets.top
+ insets.bottom + (ncomponents * h) + (ncomponents - 1)
* vgap);
}
}
/* Required by LayoutManager. */
public Dimension minimumLayoutSize(Container parent){
synchronized (parent.getTreeLock()){
//Always add the container's insets!
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int w = 0;
int h = 0;
for (int i = 0; i < ncomponents; i++){
Component comp = parent.getComponent(i);
Dimension d = comp.getMinimumSize();
if (w < d.width){
w = d.width;
}
if (h < d.height){
h = d.height;
}
}
returnnew Dimension(insets.left + insets.right + w, insets.top
+ insets.bottom + (ncomponents * h) + (ncomponents - 1)
* vgap);
}
}
/* Required by LayoutManager. */
/*
* This is called when the panel is first displayed,
* and every time its size changes.
* Note: You CAN'T assume preferredLayoutSize or
* minimumLayoutSize will be called -- in the case
* of applets, at least, they probably won't be.
*/
publicvoid layoutContainer(Container parent){
Insets insets = parent.getInsets();
int maxWidth = parent.getSize().width - (insets.left + insets.right);
/**
* parentWidth is being set by resize of scrollpane :(
* Done lots of R&D,if u have better idea let me know
*
*/
_doLayout(parent, maxWidth);
}
/**
* Do the layout
* @param parent
* @param width
*/
privatevoid _doLayout(Container parent,int width){
int nComps = parent.getComponentCount();
Insets insets = parent.getInsets();
int x = 0, y = insets.top;
/**
* parentWidth is being set by resize of scrollpane :(
* Done lots of R&D,if u have better idea let me know
*
*/
for (int i = 0; i < nComps; i++){
Component c = parent.getComponent(i);
if (c.isVisible()){
Dimension d = c.getSize();
c.setBounds(CELL_STARTX, y, width, CELLHEIGHT);
y = y + CELLHEIGHT + vgap;
}
}
}
}

