Add cell to GridBagLayout - Java Swing

Java examples for Swing:GridBagLayout

Description

Add cell to GridBagLayout

Demo Code


import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Insets;
import java.util.List;

public class Main{
    /**//from  w  ww  . ja va  2 s . c om
     * Add cell
     * @param cont
     * @param text
     * @param gridX
     * @param gridY
     * @param gridHeight
     * @param gridWidth
     * @param fill
     * @param anchor
     * @param layout
     * @param color
     */
    public static void addGbCell(Container cont, String text, int gridX,
            int gridY, int gridHeight, int gridWidth, int fill, int anchor,
            int layout, Color color) {
        CharacterSheetUtils.addGbCell(cont, text, gridX, gridY, gridHeight,
                gridWidth, fill, anchor, layout, color, null);
    }
    /**
     * Add cell
     * @param cont
     * @param text
     * @param gridX
     * @param gridY
     * @param gridHeight
     * @param gridWidth
     * @param fill
     * @param anchor
     * @param layout
     * @param color
     * @param componentList
     */
    public static void addGbCell(Container cont, String text, int gridX,
            int gridY, int gridHeight, int gridWidth, int fill, int anchor,
            int layout, Color color, List<Component> componentList) {
        java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        JPanel cell = new JPanel();
        JLabel cellText = new JLabel();
        cell.setLayout(new java.awt.FlowLayout(layout, 1, 0));
        cell.setBackground(color);
        cellText.setFont(new java.awt.Font("Dialog", 0, 10));
        cellText.setText(text);
        cell.add(cellText);
        gridBagConstraints.gridx = gridX;
        gridBagConstraints.gridy = gridY;
        gridBagConstraints.gridwidth = gridHeight;
        gridBagConstraints.gridwidth = gridWidth;
        gridBagConstraints.fill = fill;
        gridBagConstraints.anchor = anchor;
        cont.add(cell, gridBagConstraints);
        if (componentList != null) {
            componentList.add(cell);
        }
    }
}

Related Tutorials