Java JDesktopPane tileVertical(JDesktopPane desktop)

Here you can find the source of tileVertical(JDesktopPane desktop)

Description

tile Vertical

License

Open Source License

Declaration

public static void tileVertical(JDesktopPane desktop) 

Method Source Code


//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.DesktopManager;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

public class Main {
    protected static final int UNUSED_HEIGHT = 48;

    public static void tileVertical(JDesktopPane desktop) {
        DesktopManager manager = desktop.getDesktopManager();
        if (manager == null) {
            // No desktop manager - do nothing
            return;
        }//from www  . j  ava  2s  . c om

        Component[] comps = desktop.getComponents();
        Component comp;
        int count = 0;

        // Count and handle only the internal frames
        for (int i = 0; i < comps.length; i++) {
            comp = comps[i];
            if (comp instanceof JInternalFrame && comp.isVisible()) {
                count++;
            }
        }

        if (count != 0) {
            double root = Math.sqrt(count);
            int rows = (int) root;
            int columns = count / rows;
            int spares = count - (columns * rows);

            Dimension paneSize = desktop.getSize();
            int columnWidth = paneSize.width / columns;

            // We leave some space at the bottom that doesn't get covered
            int availableHeight = paneSize.height - UNUSED_HEIGHT;
            int mainHeight = availableHeight / rows;
            int smallerHeight = availableHeight / (rows + 1);
            int rowHeight = mainHeight;
            int x = 0;
            int y = 0;
            int thisRow = rows;
            int normalColumns = columns - spares;

            for (int i = comps.length - 1; i >= 0; i--) {
                comp = comps[i];
                if (comp instanceof JInternalFrame && comp.isVisible()) {
                    manager.setBoundsForFrame((JComponent) comp, x, y, columnWidth, rowHeight);
                    y += rowHeight;
                    if (--thisRow == 0) {
                        // Filled the row
                        y = 0;
                        x += columnWidth;

                        // Switch to smaller rows if necessary
                        if (--normalColumns <= 0) {
                            thisRow = rows + 1;
                            rowHeight = smallerHeight;
                        } else {
                            thisRow = rows;
                        }
                    }
                }
            }
        }
    }
}

Related

  1. arrangeMinifiedWindows(JDesktopPane desktop)
  2. cascade(JDesktopPane desktopPane, int layer)
  3. makeCenter(JDesktopPane desktop)
  4. moveOff(JDesktopPane desktop)
  5. tileHorizontal(JDesktopPane desktop)