Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPanel not using the set GridBagConstraints

821613Mar 22 2011 — edited Mar 23 2011
I am trying to get my panels set up to where I have one on top with 3 sub-panels, and then the one on the bottom. In the first panel, the middle sub-panel is supposed to extend vertically to its maximum, so that the bottom sub-panel is at the bottom, right on top of the second main panel. But right now, the program is acting like the 3 sub panels are supposed to be together at the top.

An image of what I am looking for is here: http://www.fileden.com/files/2010/3/9/2788777//jtextarea.png. What do I do?
package sscce;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class SSCCE
{
    public SSCCE()
    {
        final int colsLeft = 5, colsRight = 40 - colsLeft;
        final JFrame f = new JFrame();
        final JPanel p[] = {new JPanel(), new JPanel()};
        final JPanel subp[] = {new JPanel(), new JPanel(), new JPanel()};
        final JTextArea t[] =
            {cTA(colsLeft, "TextArea0"), cTA(colsRight, "TextArea1"),
            cTA(colsLeft, "TextArea2"), cTA(colsRight, "TextArea3"),
            cTA(colsLeft, "TextArea3"), cTA(colsRight, "TextArea4")};
        subp[0].setBackground(Color.RED);
        subp[1].setBackground(Color.GREEN);
        subp[2].setBackground(Color.BLUE);
        subp[0].add(t[0]);
        subp[0].add(t[1]);
        subp[1].add(t[2]);
        subp[1].add(t[3]);
        subp[2].add(t[4]);
        subp[2].add(t[5]);
        addItem("A1", 1, 1, p[0], subp[0], 100, 0);
        addItem("A2", 1, 1, p[0], subp[1], 100, 100);
        addItem("A3", 1, 1, p[0], subp[2], 100, 0);
        addItem("A1", 1, 1, p[1], cTA(15, "This is a textarea."), 0, 0);
        f.setSize(485, 338);
        f.setTitle("SSCCE");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.add(p[0], BorderLayout.CENTER);
        f.add(p[1], BorderLayout.SOUTH);
        f.setVisible(true);
    }
    
    private JTextArea cTA(final int cols, final String text) //cTA stands for 
                                                             //"createTextArea"
    {
        final JTextArea t = new JTextArea(text, 1, cols);
        return t;
    }
    
    public static void main(final String[] args)
    {
        new SSCCE();
    }
    
    private void addItem(final String cell, final int colSpan,
        final int rowSpan, final JPanel pan, final JComponent c,
        final int weightx, final int weighty)
    {
        final GridBagConstraints gc =
            new GridBagConstraints(cell.charAt(0) - 65, cell.charAt(1) - 49,
                colSpan, rowSpan, weightx, weighty, GridBagConstraints.EAST,
                GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0, 0);
        try
        {
            pan.add(c, gc);
            pan.validate();
        }
        catch (final Exception e)
        {
            e.printStackTrace();
            System.exit(0);
        }
        
    }
}
Edited by: Darryl Burke -- broke some extra long code lines

Edited by: ElectrifiedBrain - changed indenting from tabs to 4 spaces, set wrapping length to 80, got rid of unnecessary code.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2011
Added on Mar 22 2011
5 comments
329 views