Example usage for java.awt Container getSize

List of usage examples for java.awt Container getSize

Introduction

In this page you can find the example usage for java.awt Container getSize.

Prototype

public Dimension getSize() 

Source Link

Document

Returns the size of this component in the form of a Dimension object.

Usage

From source file:TableLayout.java

public void layoutContainer(Container parent) {
    Dimension pref = preferredLayoutSize(parent);
    int[] prefRow = this.prefRow;
    int[] prefCol = this.prefCol;
    Dimension size = parent.getSize();

    double y0;//  www  .ja v a2s  .c  om
    int yRemaining = size.height - pref.height;
    double rowWeightTotal = 0.0;
    if (yRemaining != 0 && 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.width) / 2;
    if (x0 < 0)
        x0 = 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 > 0 && i < rowWeight.length) {
            y += yRemaining * rowWeight[i] / rowWeightTotal;
        }
    }

    // TODO Auto-generated method stub

}

From source file:CenterLayout.java

/**
 * Lays out the components.//from w w  w .  jav a 2  s  .  c o m
 * 
 * @param parent
 *          the parent.
 */
public void layoutContainer(final Container parent) {

    synchronized (parent.getTreeLock()) {
        if (parent.getComponentCount() > 0) {
            final Insets insets = parent.getInsets();
            final Dimension parentSize = parent.getSize();
            final Component component = parent.getComponent(0);
            final Dimension componentSize = component.getPreferredSize();
            final int xx = insets.left
                    + (Math.max((parentSize.width - insets.left - insets.right - componentSize.width) / 2, 0));
            final int yy = insets.top + (Math
                    .max((parentSize.height - insets.top - insets.bottom - componentSize.height) / 2, 0));
            component.setBounds(xx, yy, componentSize.width, componentSize.height);
        }
    }

}

From source file:EntryLayout.java

/**
 * Compute the size of the whole mess. Serves as the guts of
 * preferredLayoutSize() and minimumLayoutSize().
 * //from  w w w  . ja  v  a  2s.c  om
 * @param parent
 *            The container in which to do the layout.
 * @param hp
 *            The horizontal padding (may be zero)
 * @param vp
 *            The Vertical Padding (may be zero).
 */
protected Dimension computeLayoutSize(Container parent, int hp, int vp) {
    if (!validWidths)
        return null;
    Component[] components = parent.getComponents();
    Dimension contSize = parent.getSize();
    int preferredWidth = 0, preferredHeight = 0;
    widths = new int[COLUMNS];
    heights = new int[components.length / COLUMNS];
    // System.out.println("Grid: " + widths.length + ", " + heights.length);

    int i;
    // Pass One: Compute largest widths and heights.
    for (i = 0; i < components.length; i++) {
        int row = i / widthPercentages.length;
        int col = i % widthPercentages.length;
        Component c = components[i];
        Dimension d = c.getPreferredSize();
        widths[col] = Math.max(widths[col], d.width);
        heights[row] = Math.max(heights[row], d.height);
    }

    // Pass two: agregate them.
    for (i = 0; i < widths.length; i++)
        preferredWidth += widths[i] + hp;
    for (i = 0; i < heights.length; i++)
        preferredHeight += heights[i] + vp;

    // Finally, pass the sums back as the actual size.
    return new Dimension(preferredWidth, preferredHeight);
}

From source file:FunLayout.java

public void layoutContainer(Container con) {
    int i, count, deltax, deltay, move;
    Dimension conSize;/*from w  w  w.  jav  a 2  s  . c  o m*/
    Rectangle rect;
    Component comp;

    conSize = con.getSize();
    if (_prevContainerSize == null) {
        _prevContainerSize = conSize;
        return;
    }
    deltax = conSize.width - _prevContainerSize.width;
    deltay = conSize.height - _prevContainerSize.height;
    _prevContainerSize = conSize;
    count = con.countComponents();
    for (i = 0; i < count; i++) {
        comp = con.getComponent(i);
        if (!comp.isVisible())
            continue;
        move = _getMove(comp);
        if (move == 0)
            continue;
        rect = comp.getBounds();
        if (_negSized.containsKey(comp)) {
            // the component is really at a negative size
            rect = (Rectangle) _negSized.get(comp);
            _negSized.remove(comp);
        }
        if ((move & MOVES_RIGHT) > 0)
            rect.x += deltax;
        else if ((move & WIDTH_CHANGES) > 0)
            rect.width += deltax;
        if ((move & MOVES_DOWN) > 0)
            rect.y += deltay;
        else if ((move & HEIGHT_CHANGES) > 0)
            rect.height += deltay;
        // if a components size becomes negative, we track it since the AWT
        // does not allow components to have a size < (0, 0)
        if (rect.width < 0 || rect.height < 0)
            _negSized.put(comp, rect);
        comp.setBounds(rect.x, rect.y, rect.width, rect.height);
    }
}

From source file:TileLayout.java

/**
 * Lays out the components, last added to first added, according to their
 * specified constraints./*from  ww w  .j av a 2  s  .c o m*/
 */
public void layoutContainer(Container parent) {
    Dimension container = parent.getSize();
    for (int i = components.size() - 1; i >= 0; i--)
        align(container, (Object[]) constraints.get(i), (Component) components.get(i));
}

From source file:DividerLayout.java

/**
 * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
 *///from  w  ww.ja va 2  s  .co m
public void layoutContainer(Container container) {
    Insets insets = container.getInsets();
    Dimension westSize = new Dimension(0, 0);
    Dimension centerSize = new Dimension(0, 0);
    Dimension eastSize = new Dimension(0, 0);
    Rectangle centerBounds = new Rectangle(0, 0, 0, 0);
    Dimension containerSize = container.getSize();
    int centerX = containerSize.width / 2;
    int centerY = containerSize.height / 2;
    if ((centerComponent != null) && (centerComponent.isVisible())) {
        centerSize = centerComponent.getPreferredSize();
        centerSize.width = Math.min(centerSize.width, containerSize.width - insets.left - insets.right);
        centerSize.height = Math.min(centerSize.height, containerSize.height - insets.top - insets.bottom);
        centerComponent.setBounds(centerX - (centerSize.width / 2), centerY - (centerSize.height / 2),
                centerSize.width, centerSize.height);
        centerBounds = centerComponent.getBounds();
    }
    if ((westComponent != null) && (westComponent.isVisible())) {
        westSize = westComponent.getPreferredSize();
    }
    if ((eastComponent != null) && (eastComponent.isVisible())) {
        eastSize = eastComponent.getPreferredSize();
    }
    /* int maxWidth = Math.min(westSize.width, eastSize.width);
     maxWidth = Math.min(maxWidth, (containerSize.width -
         centerBounds.width - insets.left -
         insets.right) / 2);*/
    int maxWidth = (containerSize.width - centerBounds.width - insets.left - insets.right) / 2;

    /* int maxHeight = Math.min(westSize.height, eastSize.height);
      maxHeight = Math.max(maxHeight, containerSize.height -
         insets.top - insets.bottom);*/
    int maxHeight = containerSize.height - insets.top - insets.bottom;
    if (westComponent != null) {
        westComponent.setBounds(centerBounds.x - maxWidth, centerY - (maxHeight / 2), maxWidth, maxHeight);
    }
    if (eastComponent != null) {
        eastComponent.setBounds(centerBounds.x + centerBounds.width, centerY - (maxHeight / 2), maxWidth,
                maxHeight);
    }
}

From source file:GraphPaperLayout.java

/**
 * Lays out the container in the specified container.
 * //ww w . j a v a  2  s.  c  o m
 * @param parent
 *          the component which needs to be laid out
 */
public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        Insets insets = parent.getInsets();
        int ncomponents = parent.getComponentCount();

        if (ncomponents == 0) {
            return;
        }

        // Total parent dimensions
        Dimension size = parent.getSize();
        int totalW = size.width - (insets.left + insets.right);
        int totalH = size.height - (insets.top + insets.bottom);

        // Cell dimensions, including padding
        int totalCellW = totalW / gridSize.width;
        int totalCellH = totalH / gridSize.height;

        // Cell dimensions, without padding
        int cellW = (totalW - ((gridSize.width + 1) * hgap)) / gridSize.width;
        int cellH = (totalH - ((gridSize.height + 1) * vgap)) / gridSize.height;

        for (int i = 0; i < ncomponents; i++) {
            Component c = parent.getComponent(i);
            Rectangle rect = compTable.get(c);
            if (rect != null) {
                int x = insets.left + (totalCellW * rect.x) + hgap;
                int y = insets.top + (totalCellH * rect.y) + vgap;
                int w = (cellW * rect.width) - hgap;
                int h = (cellH * rect.height) - vgap;
                c.setBounds(x, y, w, h);
            }
        }
    }
}

From source file:es.emergya.ui.gis.popups.GenericDialog.java

public GenericDialog(T i, final String titulo, final String icon) {
    super();/*from  w w w .  j  a v a  2  s. c  o m*/
    log.trace("GenericDialog(" + i + ")");
    setAlwaysOnTop(true);
    setResizable(false);
    setBackground(Color.WHITE);
    setPreferredSize(new Dimension(500, 500));
    setTitle(titulo);
    try {
        setIconImage(((BasicWindow) GoClassLoader.getGoClassLoader().load(BasicWindow.class)).getFrame()
                .getIconImage());
    } catch (Throwable e) {
        LOG.error("There is no icon image", e);
    }
    JPanel base = new JPanel();
    base.setLayout(new BoxLayout(base, BoxLayout.Y_AXIS));
    base.setBackground(Color.WHITE);

    JPanel title = new JPanel(new FlowLayout(FlowLayout.LEADING));

    final JLabel labelTitle = new JLabel(titulo, LogicConstants.getIcon(icon), JLabel.LEFT);
    labelTitle.setFont(LogicConstants.deriveBoldFont(12f));

    title.setBackground(Color.WHITE);

    title.add(labelTitle);

    base.add(title);

    mid = new JPanel(new SpringLayout());
    mid.setBackground(Color.WHITE);

    loadDialog(i);

    SpringUtilities.makeCompactGrid(mid, rows, cols, initialX, initialY, xPad, yPad);
    base.add(mid);

    JPanel buttons = new JPanel();

    buttons.setBackground(Color.WHITE);
    JButton accept = new JButton(i18n.getString("Buttons.ok"), LogicConstants.getIcon("button_accept"));
    accept.addActionListener(closeListener);
    accept.addActionListener(saveListener);
    buttons.add(accept);

    JButton cancel = new JButton(i18n.getString("Buttons.cancel"), LogicConstants.getIcon("button_cancel"));
    cancel.addActionListener(closeListener);
    buttons.add(cancel);

    base.add(buttons);

    getContentPane().add(base);
    pack();

    int x;
    int y;

    Container myParent;
    try {
        myParent = ((BasicWindow) GoClassLoader.getGoClassLoader().load(BasicWindow.class)).getFrame()
                .getContentPane();
        java.awt.Point topLeft = myParent.getLocationOnScreen();
        Dimension parentSize = myParent.getSize();

        Dimension mySize = getSize();

        if (parentSize.width > mySize.width)
            x = ((parentSize.width - mySize.width) / 2) + topLeft.x;
        else
            x = topLeft.x;

        if (parentSize.height > mySize.height)
            y = ((parentSize.height - mySize.height) / 2) + topLeft.y;
        else
            y = topLeft.y;

        setLocation(x, y);
    } catch (Throwable e1) {
        LOG.error("There is no basic window!", e1);
    }
}

From source file:GraphPaperTest.java

/**
 * Lays out the container in the specified container.
 * /*from   w w  w .ja  v a  2  s.c o m*/
 * @param parent
 *            the component which needs to be laid out
 */
public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        Insets insets = parent.getInsets();
        int ncomponents = parent.getComponentCount();

        if (ncomponents == 0) {
            return;
        }

        // Total parent dimensions
        Dimension size = parent.getSize();
        int totalW = size.width - (insets.left + insets.right);
        int totalH = size.height - (insets.top + insets.bottom);

        // Cell dimensions, including padding
        int totalCellW = totalW / gridSize.width;
        int totalCellH = totalH / gridSize.height;

        // Cell dimensions, without padding
        int cellW = (totalW - ((gridSize.width + 1) * hgap)) / gridSize.width;
        int cellH = (totalH - ((gridSize.height + 1) * vgap)) / gridSize.height;

        for (int i = 0; i < ncomponents; i++) {
            Component c = parent.getComponent(i);
            Rectangle rect = (Rectangle) compTable.get(c);
            if (rect != null) {
                int x = insets.left + (totalCellW * rect.x) + hgap;
                int y = insets.top + (totalCellH * rect.y) + vgap;
                int w = (cellW * rect.width) - hgap;
                int h = (cellH * rect.height) - vgap;
                c.setBounds(x, y, w, h);
            }
        }
    }
}

From source file:es.emergya.ui.gis.popups.SaveGPXDialog.java

private SaveGPXDialog(final List<Layer> capas) {
    super("Consulta de Posiciones GPS");
    setResizable(false);/*from  w w  w . j av  a  2s  . co m*/
    setAlwaysOnTop(true);
    try {
        setIconImage(((BasicWindow) GoClassLoader.getGoClassLoader().load(BasicWindow.class)).getFrame()
                .getIconImage());
    } catch (Throwable e) {
        LOG.error("There is no icon image", e);
    }

    this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    JPanel dialogo = new JPanel(new BorderLayout());
    dialogo.setBackground(Color.WHITE);
    dialogo.setBorder(new EmptyBorder(10, 10, 10, 10));

    JPanel central = new JPanel(new FlowLayout());
    central.setOpaque(false);
    final JTextField nombre = new JTextField(15);
    nombre.setEditable(false);
    central.add(nombre);
    final JButton button = new JButton("Examinar...", LogicConstants.getIcon("button_nuevo"));
    central.add(button);
    final JButton aceptar = new JButton("Guardar", LogicConstants.getIcon("button_save"));
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            if (fileChooser.showSaveDialog(SaveGPXDialog.this) == JFileChooser.APPROVE_OPTION) {
                nombre.setText(fileChooser.getSelectedFile().getAbsolutePath());
                aceptar.setEnabled(true);
            }
        }
    });

    dialogo.add(central, BorderLayout.CENTER);

    JPanel botones = new JPanel(new FlowLayout());
    botones.setOpaque(false);

    aceptar.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String base_url = nombre.getText() + "_";
            for (Layer layer : capas) {
                if (layer instanceof GpxLayer) {
                    GpxLayer gpxLayer = (GpxLayer) layer;
                    File f = new File(base_url + gpxLayer.name + ".gpx");

                    boolean sobreescribir = !f.exists();

                    try {
                        while (!sobreescribir) {
                            String original = f.getCanonicalPath();
                            f = checkFileOverwritten(nombre, f);
                            sobreescribir = !f.exists() || original.equals(f.getCanonicalPath());
                        }
                    } catch (NullPointerException t) {
                        log.debug("Cancelando creacion de fichero: " + t);
                        sobreescribir = false;
                    } catch (Throwable t) {
                        log.error("Error comprobando la sobreescritura", t);
                        sobreescribir = false;
                    }

                    if (sobreescribir) {
                        try {
                            f.createNewFile();
                        } catch (IOException e1) {
                            log.error(e1, e1);
                        }
                        if (!(f.isFile() && f.canWrite()))
                            JOptionPane.showMessageDialog(SaveGPXDialog.this,
                                    "No tengo permiso para escribir en " + f.getAbsolutePath());
                        else {
                            try {
                                OutputStream out = new FileOutputStream(f);
                                GpxWriter writer = new GpxWriter(out);
                                writer.write(gpxLayer.data);
                                out.close();
                            } catch (Throwable t) {
                                log.error("Error al escribir el gpx", t);
                                JOptionPane.showMessageDialog(SaveGPXDialog.this,
                                        "Ocurri un error al escribir en " + f.getAbsolutePath());
                            }
                        }
                    } else
                        log.error("Por errores anteriores no se escribio el fichero");
                } else
                    log.error("Una de las capas no era gpx: " + layer.name);
            }
            SaveGPXDialog.this.dispose();

        }

        private File checkFileOverwritten(final JTextField nombre, File f) throws Exception {
            String nueva = JOptionPane.showInputDialog(nombre, i18n.getString("savegpxdialog.overwrite"),
                    "Sobreescribir archivo", JOptionPane.QUESTION_MESSAGE, null, null, f.getCanonicalPath())
                    .toString();
            log.debug("Nueva ruta: " + nueva);
            return new File(nueva);
        }
    });

    JButton cancelar = new JButton("Cancelar", LogicConstants.getIcon("button_cancel"));

    cancelar.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            SaveGPXDialog.this.dispose();
        }
    });

    aceptar.setEnabled(false);
    botones.add(aceptar);
    botones.add(cancelar);
    dialogo.add(botones, BorderLayout.SOUTH);

    add(dialogo);
    setPreferredSize(new Dimension(300, 200));
    pack();

    int x;
    int y;

    Container myParent;
    try {
        myParent = ((BasicWindow) GoClassLoader.getGoClassLoader().load(BasicWindow.class)).getFrame()
                .getContentPane();
        java.awt.Point topLeft = myParent.getLocationOnScreen();
        Dimension parentSize = myParent.getSize();

        Dimension mySize = getSize();

        if (parentSize.width > mySize.width)
            x = ((parentSize.width - mySize.width) / 2) + topLeft.x;
        else
            x = topLeft.x;

        if (parentSize.height > mySize.height)
            y = ((parentSize.height - mySize.height) / 2) + topLeft.y;
        else
            y = topLeft.y;

        setLocation(x, y);
    } catch (Throwable e1) {
        LOG.error("There is no basic window!", e1);
    }

    this.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosed(WindowEvent e) {
            nombre.setText("");
            nombre.repaint();
        }

        @Override
        public void windowClosing(WindowEvent e) {
            nombre.setText("");
            nombre.repaint();
        }
    });
}