Java JPanel Child addUIComponent(JPanel jp, JComponent jc, int loc, int type, int[] decart, Insets insets)

Here you can find the source of addUIComponent(JPanel jp, JComponent jc, int loc, int type, int[] decart, Insets insets)

Description

add UI Component

License

Open Source License

Parameter

Parameter Description
jp JPanel, where to add new component
jc JComponent to add
loc location of component on the display (GridBagConstraints: CENTER, WEST, EAST, ... )
type type of component (GridBagConstraints: NONE, BOTH, HORIZONTAL, VERTICAL)
decart {cell number from left, cell number from top, cells to fill right, cells to fill bottom}
insets component border

Declaration

public static void addUIComponent(JPanel jp, JComponent jc, int loc, int type, int[] decart, Insets insets) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JPanel;

public class Main {
    /**/*from w w w.j a  v a 2s . c o  m*/
     * @param jp JPanel, where to add new component
     * @param jc JComponent to add
     * @param loc location of component on the display (GridBagConstraints: CENTER, WEST, EAST, ... )
     * @param type type of component (GridBagConstraints: NONE, BOTH, HORIZONTAL, VERTICAL)
     * @param decart {cell number from left, cell number from top, 
     *                cells to fill right, cells to fill bottom} 
     * @param insets component border
     */
    public static void addUIComponent(JPanel jp, JComponent jc, int loc, int type, int[] decart, Insets insets) {
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = loc;
        c.fill = type;
        c.gridx = decart[0];
        c.gridy = decart[1];
        c.gridwidth = decart[2];
        c.gridheight = decart[3];

        /*
         * weightx and weighty are depended on type, see below
         * 
         * type                           {weightx, weighty}         
         * ---------------------------------------------------------------------------               
         * GridBagConstraints.NONE        = 0 == {0.0   , 0.0    }   # 4-0 = 4 == 1|00|
         * GridBagConstraints.BOTH        = 1 ==   {1.0   , 1.0    }  # 4-1 = 3 == 0|11|
         * GridBagConstraints.HORIZONTAL = 2 ==   {1.0   , 0.0    }    # 4-2 = 2 == 0|10|
         * GridBagConstraints.VERTICAL     = 3 ==   {0.0   , 1.0    }  # 4-3 = 1 == 0|01|
         */
        c.weightx = (double) (((4 - type) & 2) >> 1);
        c.weighty = (double) ((4 - type) & 1);

        c.insets = insets;
        jp.add(jc, c);
    }
}

Related

  1. addSpacer(JPanel panel)
  2. addTo(JPanel panel, String label, String value)
  3. addToGridBag(Component comp, JPanel panel, int gridx, int gridy, int width, int height, int anchor)
  4. addToGridBag(final JPanel panel, final Component component, final int x, final int y)
  5. addTwoCheckBoxes(JPanel panel, JCheckBox first, boolean firstChecked, JCheckBox second, boolean secondChecked)
  6. addWidth(JPanel panel, int width)
  7. addWithPadding(JPanel parentPanel, JPanel panelToBeAdded)
  8. fillPanel(final JPanel panel, final GridBagConstraints gbc)
  9. fillPanel(JPanel panel, Component... components)