Java Swing BorderLayout createBorderLayoutPane(Object... args)

Here you can find the source of createBorderLayoutPane(Object... args)

Description

create Border Layout Pane

License

Open Source License

Declaration

public static JPanel createBorderLayoutPane(Object... args) 

Method Source Code

//package com.java2s;
import javax.swing.*;

import java.awt.*;

public class Main {
    private static final int CASE_FOUR = 4;

    public static JPanel createBorderLayoutPane(Object... args) {
        if (args.length % 2 != 0) {
            throw new IllegalArgumentException("Illegal Arguments");
        }/*  ww w .  j a v  a 2 s .c o  m*/
        BorderLayout layout = new BorderLayout();
        Object maybeHgap = args[args.length - 2];
        Object maybeVgap = args[args.length - 1];
        boolean hasGap = false;
        if (maybeHgap instanceof Integer && maybeVgap instanceof Integer) {
            layout.setHgap((Integer) maybeHgap);
            layout.setVgap((Integer) maybeVgap);
            hasGap = true;
        }
        JPanel pane = new JPanel(layout);
        pane.setOpaque(false);
        for (int i = 0; i < (hasGap ? args.length - 2 : args.length) / 2; i++) {
            pane.add((Component) args[2 * i], args[2 * i + 1]);
        }
        return pane;
    }

    public static JPanel createBorderLayoutPane(Component[] components) {
        JPanel pane = new JPanel(new BorderLayout());
        for (int i = 0, len = components.length; i < len; i++) {
            switch (i) {
            case 0:
                pane.add(components[0], BorderLayout.CENTER);
                break;
            case 1:
                if (components[1] != null) {
                    pane.add(components[1], BorderLayout.EAST);
                }
                break;
            case 2:
                if (components[2] != null) {
                    pane.add(components[2], BorderLayout.SOUTH);
                }
                break;
            case 3:
                if (components[3] != null) {
                    pane.add(components[3], BorderLayout.WEST);
                }
                break;
            case CASE_FOUR:
                if (components[CASE_FOUR] != null) {
                    pane.add(components[CASE_FOUR], BorderLayout.NORTH);
                }
                break;
            default:
                pane.add(components[0], BorderLayout.CENTER);
            }
        }
        return pane;
    }
}

Related

  1. addBorders(Component content)
  2. addToPanel(JPanel panel, JComponent centerComponent, JComponent arrangedComponent, String borderLayoutConstraint)
  3. addToVerticalBorderLayout(JComponent north, JComponent center, JComponent south)
  4. createBox(Border border, Component center, Component north)
  5. createPanel(Component comp, boolean setBorder, int borderType)
  6. createPanel(JComponent centerComponent, JComponent placedComponent, String borderLayoutConstraint)
  7. layoutCompactHorizontal(JComponent... list)