Busted tile algorithm.
The following piece of code from http://www.java2s.com/Code/Java/Swing-JFC/InternalFrameTest.htm does not seem to work as all of the frames are minimized... any one know what gives?
publicvoid tileWindows(){
JInternalFrame[] frames = desktop.getAllFrames();
// count frames that aren't iconized
int frameCount = 0;
for (int i = 0; i < frames.length; i++){
if (!frames[i].isIcon())
frameCount++;
}
int rows = (int) Math.sqrt(frameCount);
int cols = frameCount / rows;
int extra = frameCount % rows;
// number of columns with an extra row
int width = desktop.getWidth() / cols;
int height = desktop.getHeight() / rows;
int r = 0;
int c = 0;
for (int i = 0; i < frames.length; i++){
if (!frames[i].isIcon()){
try{
frames[i].setMaximum(false);
frames[i].reshape(c * width, r * height, width, height);
r++;
if (r == rows){
r = 0;
c++;
if (c == cols - extra){// start adding an extra row
rows++;
height = desktop.getHeight() / rows;
}
}
}catch (PropertyVetoException e){
}
}
}
}
[2451 byte] By [
javaroba] at [2007-11-26 12:19:42]

# 5
I'm having the same problem with this similar code that I found, where the windows remain small, ( not minimized ).
Any ideas on how I can get the windows stretched, as tiling normally does?
public static void tile(JDesktopPane desktopPane, int layer)
{
JInternalFrame[] frames = desktopPane.getAllFramesInLayer(layer);
if (frames.length == 0)
{
return;
}
tile(frames, desktopPane.getBounds());
}
public static void tile(JDesktopPane desktopPane)
{
JInternalFrame[] frames = desktopPane.getAllFrames();
if (frames.length == 0)
{
return;
}
tile(frames, desktopPane.getBounds());
}
private static void tile(JInternalFrame[] frames, Rectangle dBounds)
{
int cols = (int) Math.sqrt(frames.length);
int rows = (int) (Math.ceil(((double) frames.length) / cols));
int lastRow = frames.length - cols * (rows - 1);
int width, height;
if (lastRow == 0)
{
rows--;
height = dBounds.height / rows;
} else
{
height = dBounds.height / rows;
if (lastRow < cols)
{
rows--;
width = dBounds.width / lastRow;
for (int i = 0; i < lastRow; i++)
{
frames[cols * rows + i].setBounds(i * width, rows * height,
width, height);
}
}
}
width = dBounds.width / cols;
for (int j = 0; j < rows; j++)
{
for (int i = 0; i < cols; i++)
{
frames[i + j * cols].setBounds(i * width, j * height,
width, height);
}
}
}