Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with GridBagLayout

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with GridBagLayout

    This is my first attempt at using the GridBagLayout. Here is a screen shot of the program I made with C# that I am trying to remake in Java:
    GBL.jpg


    Here is my code:
    package ISAPI;
     
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
     
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
     
    public class MainFrame extends JFrame 
    {
     
    	final int WIDTH = 850;
    	final int HEIGHT = 600;
    	private String[] ComboArry ={" ", "IAgentManagement", "IContactManagment", "ISessionManagement"};
    	public MainFrame(String title)
    	{
    		super(title);
    		Dimension size = getPreferredSize();
    		size.width = WIDTH;
    		size.height = HEIGHT;
    		setPreferredSize(size);
     
    		JLabel hostLabel = new JLabel("IS Host");
    		JLabel generatedLabel = new JLabel("Generated URL to Post to integration Services");
    		JLabel responseHtmlLabel = new JLabel("Response from the API in HTML");
    		JLabel responseTextLabel = new JLabel("Response from the API in Text");
    		JLabel interfaceLabel = new JLabel("Interface");
     
    		JTextField hostTextField = new JTextField(22);
     
    		JTextArea generatedTextArea = new JTextArea(3, 20);
    		JTextArea responseHTMLTextArea = new JTextArea(10, 10);
    		JTextArea responseTextTextArea = new JTextArea(10, 4);
     
    		JButton createURLButton = new JButton("Create URL");
    		JButton postButton = new JButton("Post");
     
    		JComboBox interfaceBox = new JComboBox(ComboArry);
    		interfaceBox.setPreferredSize(new Dimension(250,20));
     
    		ActivityPanel Ap = new ActivityPanel();
     
    		Container pane = getContentPane();
    		pane.setLayout(new GridBagLayout());
    		GridBagConstraints gc = new GridBagConstraints();
     
    		//first row
     
    		gc.fill = GridBagConstraints.HORIZONTAL;
    		gc.weightx = 0.2;
    		gc.weighty = 1;
    		gc.gridx = 0;
    		gc.gridy = 0;
    		add(hostLabel, gc);
     
     
    		gc.fill = GridBagConstraints.HORIZONTAL;
    		gc.weightx = 0.5;
    		gc.gridx = 1;
    		gc.gridy = 0;
    		add(hostTextField, gc);
     
    		gc.fill = GridBagConstraints.HORIZONTAL;
    		gc.weightx = 2;
    		gc.gridx = 2;
    		gc.gridy = 0;
    		add(generatedLabel, gc);
     
     
    		//second row
    		gc.fill = GridBagConstraints.HORIZONTAL;
    		gc.weightx = 0.5;
    		gc.weighty = 1;
    		gc.gridx = 0;
    		gc.gridy = 1;
    		add(interfaceLabel, gc);
     
    		gc.fill = GridBagConstraints.HORIZONTAL;
    		gc.weightx = 0.5;
    		gc.gridx = 1;
    		gc.gridy = 1;
    		add(interfaceBox, gc);
     
    		gc.fill = GridBagConstraints.HORIZONTAL;
    		gc.weightx = 0.5;
    		gc.weighty = 10;
    		gc.gridx = 2;
    		gc.gridy = 1;
    		add(generatedTextArea, gc);
     
    		//third row
    		gc.fill = GridBagConstraints.HORIZONTAL;
    		gc.weightx = 0.5;
    		gc.weighty = 10;
    		gc.gridx =0;
    		gc.gridy = 2;
    		add(Ap, gc);
     
     
     
    	}
    }

    Here is a screen shot of what I have so far:
    GBL.jpg

    The ActivityPanel has a set width and height of 250 x 450 with a red background(which I did just to see what was going on).

    I tried using the GridBagConstraint Constants to no avail.

    is there a way to set a margin or padding in between cells?

    basically I am lost, I have to use the gridbaglayout, I went through the demo at docs.oracle.com, if someone could point me in the right direction I would appreciate it.

    Thanks in advance!


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Help with GridBagLayout

    What I've noticed is that GridBagLayout seems to care nothing about the sizes you set for your Components. Instead, you have to be tricky with your organization.

    GridBagConstraints also has these variables:

    gridwidth and gridheight (which allow a Component to span more than 1 cell by 1 cell; for example, gridheight = 2 would make a Component span two cells vertically in the GridBagLayout)
    ipadx and ipady (internal padding, which makes a component larger or smaller).
    anchor (controls where a Component will be aligned in its grid area)

    I think you should try messing with ipadx and ipady. You could use them to make your JTextAreas fill large areas of the JFrame. Also note that you can set

    <GridBagConstraints>.fill = GridBagConstraints.BOTH;

    This will make a Component fill its area both horizontally and vertically.

    anchor and gridwidth/gridheight could be beneficial to you, too.

    Here's a quick tip: if you want to create a space between all of your Components and the edge of a JFrame, use an EmptyBorder.
    Last edited by snowguy13; February 20th, 2012 at 06:55 AM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. GridBagLayout can't seem to get it right.
    By ToyYoda in forum AWT / Java Swing
    Replies: 1
    Last Post: August 24th, 2011, 02:19 AM
  2. GridBagLayout help
    By mjpam in forum AWT / Java Swing
    Replies: 1
    Last Post: May 8th, 2011, 10:51 AM
  3. GridBagLayout display problem
    By mjpam in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 5th, 2011, 04:58 PM
  4. GridBagLayout help
    By mjpam in forum Java Theory & Questions
    Replies: 2
    Last Post: August 19th, 2010, 06:35 PM
  5. [SOLVED] Problem with GridBagLayout
    By lumpy in forum AWT / Java Swing
    Replies: 3
    Last Post: February 28th, 2010, 11:58 AM