GridBagLayout question

My main class holds frame and gets left and right panels from other classes

/**
	 * Matthew Schrum
	 * 11/14/2010
	 */

import java.util.Random;
import javax.swing.JFrame;

public class Sim {

	public static void main(String[] args) {
		
		//variables
		LeftPanel leftPanel = new LeftPanel();
		RightPanel rightPanel = new RightPanel();
		
		//GUI
		JFrame frame = new JFrame("Biz Sim");
		frame.getContentPane().add(leftPanel.getPanel());
		frame.getContentPane().add(rightPanel.getPanel());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocation(400, 200);
		frame.setSize(600, 500);
		frame.setResizable(false);
		frame.setVisible(true);
		
	}//end main
}//end class

Left panel holds only a large text area for outputting results of calculations and whatnot from the program running.

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class LeftPanel {

	JTextArea txtArea = new JTextArea(300, 100);
	JPanel leftPanel = new JPanel(new GridBagLayout());
	
	public LeftPanel(){

		txtArea.setText("TESTING 12345678910111213141516171819");
		GridBagConstraints c = new GridBagConstraints();
	      c.gridx=0;
	      c.gridy=0;
	      c.weightx=1;
	      c.weighty=1;
	      c.gridwidth=2;
	      c.fill = GridBagConstraints.BOTH;
	      leftPanel.add(txtArea,c);
	}
	
	public JPanel getPanel() {
		return leftPanel;
	}
	public void setPanel (JPanel leftPanel) {
		this.leftPanel = leftPanel;
	}
	
}

Right panel holds buttons and one smaller text area below. The right panel will switch between buttons available (i.e. clicking inventory button will open inventory interface in the place of the main interface)

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class RightPanel {

	JButton btnMain = new JButton("Main Menu");
	JButton btnInv = new JButton("Inventory Menu");
	JButton btnEmp = new JButton("Employee Info");
	JTextArea txtArea = new JTextArea("Stats:");
	JPanel rightPanel = new JPanel(new GridBagLayout());
	
	
	public RightPanel(){
		
		btnMain.setPreferredSize(new Dimension(120, 20));
		btnEmp.setPreferredSize(new Dimension(120, 20));
		btnInv.setPreferredSize(new Dimension(120, 20));
		
		GridBagConstraints c = new GridBagConstraints();
	      c.gridx=1;
	      c.gridy=0;
	      c.gridwidth=1;
	      c.weightx=0;
	      c.weighty=1;
	      c.fill = GridBagConstraints.NONE;
	      rightPanel.add(btnMain,c);
	      
	      c.gridx=1;
	      c.gridy=1;
	      c.gridwidth=1;
	      c.weightx=0;
	      c.weighty=1;
	      c.fill = GridBagConstraints.NONE;
	      rightPanel.add(btnEmp,c);

	      c.gridx=1;
	      c.gridy=2;
	      c.gridwidth=1;
	      c.weightx=0;
	      c.weighty=1;
	      c.fill = GridBagConstraints.NONE;
	      rightPanel.add(btnInv,c);
	      
		    btnMain.addActionListener (
			         new ActionListener() {
			            public void actionPerformed(ActionEvent e) {
			            	
			            }
			         }
			);
	      
		    btnEmp.addActionListener (
			         new ActionListener() {
			            public void actionPerformed(ActionEvent e) {

			            }
			         }
			);
			
		    btnInv.addActionListener (
			         new ActionListener() {
			            public void actionPerformed(ActionEvent e) {

			            }
			         }
			);
		
	}
	
	public JPanel getPanel() {
		return rightPanel;
	}
	public void setPanel (JPanel rightPanel) {
		this.rightPanel = rightPanel;
	}
	
}

Right now I'm just trying to get the Gui set up. I'm new to java and don't know all the syntax and whatnot just yet, so I'm guessing it's a simple syntax error somewhere. Anyways, my buttons show up, but they are spread out in the middle of the frame and the text area doesn't show up at all.

What I need help on:
Getting the buttons to remain in the top-right.
Getting the left panel's text area to show up and cover half the frame.

Thank you muchly to those who help

Edited 5 Years Ago by Slyvr: n/a

set layout for JFrame for example:

//GUI
        JFrame frame = new JFrame("Biz Sim");
        frame.setLayout(new GridLayout(1,0));

for JFrame default layout is:

System.out.println(frame.getLayout());

java.awt.BorderLayout[hgap=0,vgap=0]

Edited 5 Years Ago by quuba: n/a

set layout for JFrame for example:

//GUI
        JFrame frame = new JFrame("Biz Sim");
        frame.setLayout(new GridLayout(1,0));

for JFrame default layout is:

System.out.println(frame.getLayout());

java.awt.BorderLayout[hgap=0,vgap=0]

Excellent! This worked. I'm having trouble now trying to get a button and text field aligned next to each other on top of a text area on the left panel.
What's wrong here?

LEFT PANEL

txtArea.setPreferredSize(new Dimension(280,430));
		txtMoney.setPreferredSize(new Dimension(120,20));
		btnDay.setPreferredSize(new Dimension(120,20));
c.gridx=0;
	      c.gridy=0;
	      c.gridwidth=2;
	      c.weightx=0;
	      c.weighty=0;
	      c.fill = GridBagConstraints.NONE;
	      leftPanel.add(btnDay,c);
		
	      c.gridx=1;
	      c.gridy=0;
	      c.gridwidth=2;
	      c.weightx=0;
	      c.weighty=0;
	      c.fill = GridBagConstraints.NONE;
	      leftPanel.add(txtMoney,c);
	      
	      c.gridx=0;
	      c.gridy=1;
	      c.gridwidth=1;
	      c.weightx=0;
	      c.weighty=1;
	      c.fill = GridBagConstraints.NONE;
	      leftPanel.add(txtArea,c);

Gridbaglayout confuses the hell out of me but it seems like the most adaptable layout to put things exactly where I want them. There really aren't any good resources explaining gridbaglayout online :( Or perhaps, have suggestions for a better layout?

maybe you want to explore setBounds().

This will put your elements to where you want them to be.

This article has been dead for over six months. Start a new discussion instead.