Example usage for java.awt Container add

List of usage examples for java.awt Container add

Introduction

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

Prototype

public void add(Component comp, Object constraints) 

Source Link

Document

Adds the specified component to the end of this container.

Usage

From source file:layout.GridBagLayoutDemo.java

public static void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }//  w w  w .j  av  a  2 s  . c  om

    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
        //natural height, maximum width
        c.fill = GridBagConstraints.HORIZONTAL;
    }

    button = new JButton("Button 1");
    if (shouldWeightX) {
        c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 3");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 2;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Long-Named Button 4");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 40; //make this component tall
    c.weightx = 0.0;
    c.gridwidth = 3;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(button, c);

    button = new JButton("5");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 0; //reset to default
    c.weighty = 1.0; //request any extra vertical space
    c.anchor = GridBagConstraints.PAGE_END; //bottom of space
    c.insets = new Insets(10, 0, 0, 0); //top padding
    c.gridx = 1; //aligned with button 2
    c.gridwidth = 2; //2 columns wide
    c.gridy = 2; //third row
    pane.add(button, c);
}

From source file:Main.java

/**
 * Lays out a list of controls in a compact grid-layout, meaning that each
 * cell in the grid only takes up it's preferred size.
 * //from   w ww . j a va2s. co  m
 * @param parent the parent <code>Container</code>
 * @param colCount the number of columns
 * @param padding the number of pixels to pad between cells
 * @param components a <code>List</code> of <code>Component</code>s that
 *        should be added to <code>parent</code>
 */
public static void makeCompactGrid(Container parent, int colCount, int padding,
        List<? extends Component> components) {

    parent.setLayout(new GridBagLayout());

    final int componentCount = components.size();
    final int rowCount = getRowCount(componentCount, colCount);

    final GridBagConstraints c = new GridBagConstraints();
    final int realPadding = (padding / 2);

    c.gridheight = 1;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 1;
    c.insets = new Insets(realPadding, realPadding, realPadding, realPadding);

    for (int i = 0; i < (rowCount * colCount); i++) {
        final int x = (i % colCount);
        final int y = (i / colCount);

        c.gridx = x;
        c.gridy = y;

        parent.add(components.get(i), c);
    }
}

From source file:Main.java

/**
 * Gridbaglayout helper method./*from   ww w.  j  av a  2  s  . c  o m*/
 * @param container container that component gets added on.
 * @param component component to be added and set constraints for.
 * @param gridx GridBagConstraints argument.
 * @param gridy GridBagConstraints argument.
 * @param gridwidth GridBagConstraints argument.
 * @param gridheight GridBagConstraints argument.
 * @param weightx GridBagConstraints argument.
 * @param weighty GridBagConstraints argument.
 * @param anchor GridBagConstraints argument.
 * @param fill GridBagConstraints argument.
 * @param insets GridBagConstraints argument.
 * @param ipadx GridBagConstraints argument.
 * @param ipady GridBagConstraints argument.
 */
public static void addWithAllGridBagConstraints(final Container container, final Component component,
        final int gridx, final int gridy, final int gridwidth, final int gridheight, final double weightx,
        final double weighty, final int anchor, final int fill, final Insets insets, final int ipadx,
        final int ipady) {
    GridBagConstraints gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = gridx;
    gridBagConstraints.gridy = gridy;
    gridBagConstraints.gridheight = gridheight;
    gridBagConstraints.gridwidth = gridwidth;
    gridBagConstraints.weightx = weightx;
    gridBagConstraints.weighty = weighty;
    gridBagConstraints.anchor = anchor;
    gridBagConstraints.fill = fill;
    gridBagConstraints.insets = insets;
    gridBagConstraints.ipadx = ipadx;
    gridBagConstraints.ipady = ipady;
    container.add(component, gridBagConstraints);
}

From source file:BorderLayout1.java

public void init() {
    Container cp = getContentPane();
    cp.add(BorderLayout.NORTH, new JButton("North"));
    cp.add(BorderLayout.SOUTH, new JButton("South"));
    cp.add(BorderLayout.EAST, new JButton("East"));
    cp.add(BorderLayout.WEST, new JButton("West"));
    cp.add(BorderLayout.CENTER, new JButton("Center"));
}

From source file:Box1.java

public void init() {
    Box bv = Box.createVerticalBox();
    for (int i = 0; i < 5; i++)
        bv.add(new JButton("bv " + i));
    Box bh = Box.createHorizontalBox();
    for (int i = 0; i < 5; i++)
        bh.add(new JButton("bh " + i));
    Container cp = getContentPane();
    cp.add(BorderLayout.EAST, bv);
    cp.add(BorderLayout.SOUTH, bh);
}

From source file:Box2.java

public void init() {
    Box bv = Box.createVerticalBox();
    for (int i = 0; i < 5; i++) {
        bv.add(new JButton("bv " + i));
        bv.add(Box.createVerticalStrut(i * 10));
    }/* www. java  2 s  .  c  o m*/
    Box bh = Box.createHorizontalBox();
    for (int i = 0; i < 5; i++) {
        bh.add(new JButton("bh " + i));
        bh.add(Box.createHorizontalStrut(i * 10));
    }
    Container cp = getContentPane();
    cp.add(BorderLayout.EAST, bv);
    cp.add(BorderLayout.SOUTH, bh);
}

From source file:TabbedPane1.java

public void init() {
    for (int i = 0; i < flavors.length; i++)
        tabs.addTab(flavors[i], new JButton("Tabbed pane " + i));
    tabs.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            txt.setText("Tab selected: " + tabs.getSelectedIndex());
        }//from   w w w  . j  a  va2  s  .c om
    });
    Container cp = getContentPane();
    cp.add(BorderLayout.SOUTH, txt);
    cp.add(tabs);
}

From source file:Test.java

public Test() {
    Container container = this.getContentPane();
    container.add("Center", new NumericShaperPanel());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(250, 120);
    this.setVisible(true);
}

From source file:Main.java

public Main() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    genderGroup.add(genderMale);/*w  ww. jav  a  2  s  .  c o  m*/
    genderGroup.add(genderFemale);
    genderGroup.add(genderUnknown);

    Box b1 = Box.createVerticalBox();
    b1.add(genderMale);
    b1.add(genderFemale);
    b1.add(genderUnknown);

    Container contentPane = this.getContentPane();
    contentPane.add(b1, BorderLayout.CENTER);
}

From source file:Main.java

public Main() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    String[] sList = new String[] { "Spring", "Summer", "Fall", "Winter" };
    JComboBox<String> seasons = new JComboBox<>(sList);

    seasons.addItemListener((ItemEvent e) -> {
        Object item = e.getItem();
        if (e.getStateChange() == ItemEvent.SELECTED) {
            // Item has been selected
            System.out.println(item + "  has  been  selected");
        } else if (e.getStateChange() == ItemEvent.DESELECTED) {
            // Item has been deselected
            System.out.println(item + "  has  been  deselected");
        }//from   w w  w  .j  a v  a  2  s .com
    });

    Container contentPane = this.getContentPane();
    contentPane.add(seasons, BorderLayout.CENTER);
}