Example usage for java.awt GridBagConstraints NORTH

List of usage examples for java.awt GridBagConstraints NORTH

Introduction

In this page you can find the example usage for java.awt GridBagConstraints NORTH.

Prototype

int NORTH

To view the source code for java.awt GridBagConstraints NORTH.

Click Source Link

Document

Put the component at the top of its display area, centered horizontally.

Usage

From source file:Main.java

public static void main(String arg[]) {
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());

    JPanel gb = new JPanel(new GridBagLayout());
    JLabel content = new JLabel("Some text");

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.weighty = 1;/*  w  ww .  j a v a2  s.  c om*/

    gb.add(content, gbc); // gbc is containing the GridBagConstraints
    frame.add(gb, BorderLayout.CENTER);

    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame(Main.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel btmPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weighty = 1.0;/*from   w ww .  j  a  va  2s .c  o m*/
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = GridBagConstraints.NORTH;
    JButton comp = new JButton("Panel-1");
    btmPanel.add(comp, gbc);
    JButton comp2 = new JButton("Panel-2");
    btmPanel.add(comp2, gbc);
    JButton comp3 = new JButton("Panel-3");
    comp3.setPreferredSize(new Dimension(comp.getPreferredSize().width, comp.getPreferredSize().height + 10));
    btmPanel.add(comp3, gbc);
    frame.add(btmPanel);
    frame.pack();
    frame.setVisible(true);
}

From source file:GridBagWithAnchor.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of anchor constraints");
    JPanel p = new JPanel(new GridBagLayout());

    p.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;//from   ww  w  . java2  s  .c  om
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2;
    c.anchor = GridBagConstraints.NORTH; // place component on the North
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.anchor = GridBagConstraints.SOUTHWEST;
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1;
    c.anchor = GridBagConstraints.CENTER; // remember to rest to center
    p.add(new JButton("and"), c);
    c.gridx = 2;
    p.add(new JButton("Support !!!"), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:Main.java

public static JPanel createVertical(final Component... components) {
    final JPanel jp = new JPanel(new GridBagLayout());
    final GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;// w w  w  .j  av a2 s  .com
    gbc.gridy = 0;
    gbc.insets = new Insets(0, 5, 4, 5);
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    for (final Component component : components) {
        if (gbc.gridy == components.length - 1) {
            gbc.weighty = 1.0;
        }
        jp.add(component, gbc);
        gbc.gridy++;
    }
    return jp;
}

From source file:Main.java

public static void addComponent(final Container cont, final GridBagLayout gbl, final Component c, final int x,
        final int y, final int width, final int height, final double weightx, final double weighty) {
    addComponent(cont, gbl, c, x, y, width, height, weightx, weighty, GridBagConstraints.NORTH,
            GridBagConstraints.BOTH);
}

From source file:Main.java

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

    this.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.anchor = GridBagConstraints.NORTH;
    gbc.weighty = 0.0;// w ww.  j a v  a  2 s  . c  o m

    JPanel one = new JPanel();
    one.setPreferredSize(new Dimension(200, 200));
    one.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    JPanel two = new JPanel();
    two.setPreferredSize(new Dimension(200, 200));
    two.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    this.add(one, gbc);

    gbc.gridy = 1;
    gbc.weighty = 0.0;
    gbc.fill = GridBagConstraints.VERTICAL;

    this.add(two, gbc);

    this.pack();
    this.setVisible(true);
}

From source file:PizzaGridBagLayout.java

public PizzaGridBagLayout() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridBagLayout());
    addItem(panel1, new JLabel("Name:"), 0, 0, 1, 1, GridBagConstraints.EAST);
    addItem(panel1, new JLabel("Phone:"), 0, 1, 1, 1, GridBagConstraints.EAST);
    addItem(panel1, new JLabel("Address:"), 0, 2, 1, 1, GridBagConstraints.EAST);

    addItem(panel1, name, 1, 0, 2, 1, GridBagConstraints.WEST);
    addItem(panel1, phone, 1, 1, 1, 1, GridBagConstraints.WEST);
    addItem(panel1, address, 1, 2, 2, 1, GridBagConstraints.WEST);

    Box sizeBox = Box.createVerticalBox();
    ButtonGroup sizeGroup = new ButtonGroup();
    sizeGroup.add(small);//www  .java2 s.  co  m
    sizeGroup.add(medium);
    sizeGroup.add(large);
    sizeBox.add(small);
    sizeBox.add(medium);
    sizeBox.add(large);
    sizeBox.setBorder(BorderFactory.createTitledBorder("Size"));
    addItem(panel1, sizeBox, 0, 3, 1, 1, GridBagConstraints.NORTH);

    Box styleBox = Box.createVerticalBox();

    ButtonGroup styleGroup = new ButtonGroup();
    styleGroup.add(thin);
    styleGroup.add(thick);
    styleBox.add(thin);
    styleBox.add(thick);
    styleBox.setBorder(BorderFactory.

            createTitledBorder("Style"));
    addItem(panel1, styleBox, 1, 3, 1, 1, GridBagConstraints.NORTH);

    Box topBox = Box.createVerticalBox();
    ButtonGroup topGroup = new ButtonGroup();
    topGroup.add(pepperoni);
    topGroup.add(mushrooms);
    topGroup.add(anchovies);
    topBox.add(pepperoni);
    topBox.add(mushrooms);
    topBox.add(anchovies);
    topBox.setBorder(BorderFactory.createTitledBorder("Toppings"));
    addItem(panel1, topBox, 2, 3, 1, 1, GridBagConstraints.NORTH);

    Box buttonBox = Box.createHorizontalBox();
    buttonBox.add(okButton);
    buttonBox.add(Box.createHorizontalStrut(20));
    buttonBox.add(closeButton);
    addItem(panel1, buttonBox, 2, 4, 1, 1, GridBagConstraints.NORTH);

    this.add(panel1);
    this.pack();
    this.setVisible(true);
}

From source file:org.apache.marmotta.splash.common.ui.MessageDialog.java

public static void show(String title, String message, String description) {
    final JDialog dialog = new JDialog((Frame) null, title);
    dialog.setModal(true);/*from   w w  w.j  a  v a  2 s .co m*/
    dialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

    final JPanel root = new JPanel(new GridBagLayout());
    root.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    dialog.getRootPane().setContentPane(root);

    final JButton close = new JButton("OK");
    close.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
        }
    });
    GridBagConstraints cClose = new GridBagConstraints();
    cClose.gridx = 0;
    cClose.gridy = 2;
    cClose.gridwidth = 2;
    cClose.weightx = 1;
    cClose.weighty = 0;
    cClose.insets = new Insets(5, 5, 5, 5);

    root.add(close, cClose);
    dialog.getRootPane().setDefaultButton(close);

    Icon icon = loadIcon(MARMOTTA_ICON);
    if (icon != null) {
        JLabel lblIcn = new JLabel(icon);

        GridBagConstraints cIcon = new GridBagConstraints();
        cIcon.gridx = 1;
        cIcon.gridy = 0;
        cIcon.gridheight = 2;
        cIcon.fill = GridBagConstraints.NONE;
        cIcon.weightx = 0;
        cIcon.weighty = 1;
        cIcon.anchor = GridBagConstraints.NORTH;
        cIcon.insets = new Insets(10, 5, 5, 0);
        root.add(lblIcn, cIcon);
    }

    JLabel lblMsg = new JLabel("<html>" + StringEscapeUtils.escapeHtml3(message).replaceAll("\\n", "<br>"));
    lblMsg.setFont(lblMsg.getFont().deriveFont(Font.BOLD, 16f));
    GridBagConstraints cLabel = new GridBagConstraints();
    cLabel.gridx = 0;
    cLabel.gridy = 0;
    cLabel.fill = GridBagConstraints.BOTH;
    cLabel.weightx = 1;
    cLabel.weighty = 0.5;
    cLabel.insets = new Insets(5, 5, 5, 5);
    root.add(lblMsg, cLabel);

    JLabel lblDescr = new JLabel(
            "<html>" + StringEscapeUtils.escapeHtml3(description).replaceAll("\\n", "<br>"));
    cLabel.gridy++;
    cLabel.insets = new Insets(0, 5, 5, 5);
    root.add(lblDescr, cLabel);

    dialog.pack();
    dialog.setLocationRelativeTo(null);

    dialog.setVisible(true);
    dialog.dispose();
}

From source file:com.sec.ose.osi.ui.frm.main.report.JPanReportMain.java

/**
 * This method initializes this//  w  w  w  . j  av  a 2 s .  co  m
 * 
 * @return void
 */
private void initialize() {
    this.setSize(482, 200);
    this.setLayout(new GridBagLayout());

    GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
    gridBagConstraints11.fill = GridBagConstraints.BOTH;
    gridBagConstraints11.weighty = 1.0;
    gridBagConstraints11.weightx = 1.0;
    gridBagConstraints11.gridx = 0;
    gridBagConstraints11.gridy = 0;
    GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
    gridBagConstraints1.gridx = 10;
    gridBagConstraints1.insets = new Insets(10, 0, 0, 0);
    gridBagConstraints1.anchor = GridBagConstraints.NORTH;
    gridBagConstraints1.gridy = 0;

    this.add(getJSplitPane(), gridBagConstraints11);
    this.add(getJPanelbutton(), gridBagConstraints1);

}

From source file:com.digitalgeneralists.assurance.ui.components.FilePickerTextField.java

protected void initializeComponent() {
    if (!this.initialized) {
        this.filePicker.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        GridBagLayout gridbag = new GridBagLayout();
        this.setLayout(gridbag);

        final JPanel filePathPanel = new JPanel();
        filePathPanel.setLayout(new GridBagLayout());

        GridBagConstraints filePathPanelConstraints = new GridBagConstraints();
        filePathPanelConstraints.anchor = GridBagConstraints.NORTH;
        filePathPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
        filePathPanelConstraints.gridx = 0;
        filePathPanelConstraints.gridy = 0;
        filePathPanelConstraints.weightx = 1.0;
        filePathPanelConstraints.weighty = 1.0;
        filePathPanelConstraints.gridheight = 1;
        filePathPanelConstraints.gridwidth = 2;
        filePathPanelConstraints.insets = new Insets(0, 0, 0, 0);

        GridBagConstraints pathTextFieldConstraints = new GridBagConstraints();
        pathTextFieldConstraints.anchor = GridBagConstraints.NORTH;
        pathTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
        pathTextFieldConstraints.gridx = 0;
        pathTextFieldConstraints.gridy = 0;
        pathTextFieldConstraints.weightx = 0.9;
        pathTextFieldConstraints.weighty = 1.0;
        pathTextFieldConstraints.gridheight = 1;
        pathTextFieldConstraints.gridwidth = 1;
        pathTextFieldConstraints.insets = new Insets(0, 0, 0, 0);

        GridBagConstraints pathFileChooserButtonConstraints = new GridBagConstraints();
        pathFileChooserButtonConstraints.anchor = GridBagConstraints.NORTH;
        pathFileChooserButtonConstraints.fill = GridBagConstraints.HORIZONTAL;
        pathFileChooserButtonConstraints.gridx = 1;
        pathFileChooserButtonConstraints.gridy = 0;
        pathFileChooserButtonConstraints.weightx = 0.1;
        pathFileChooserButtonConstraints.weighty = 1.0;
        pathFileChooserButtonConstraints.gridheight = 1;
        pathFileChooserButtonConstraints.gridwidth = 1;
        pathFileChooserButtonConstraints.insets = new Insets(0, 0, 0, 0);

        filePathPanel.add(this.pathTextField, pathTextFieldConstraints);
        filePathPanel.add(this.pathFileChooserButton, pathFileChooserButtonConstraints);
        this.add(filePathPanel, filePathPanelConstraints);
        this.pathFileChooserButton.setActionCommand(AssuranceActions.chooseFilePathAction);
        this.pathTextField.getDocument().addDocumentListener(this.textPropertyValidationListener);
        this.pathFileChooserButton.addActionListener(this);

        this.initialized = true;
    }/*w w w  .j av a2  s .c  om*/
}