Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridBagLayout Distribution Problem 1

Status
Not open for further replies.

YerMom

Programmer
Oct 3, 2006
127
US
I'm making a reusable component that consists of a JPanel that contains a JButton and a JTextField.
The JButton and JTextField need to be at the left edge of the JPanel.
The JButon must appear on one row and the JTextField must appear in the row below the JButton.

I want the JTextField to span the width of the JPanel.
I want the JTextField to be resizable so I decided to try the GridBagLayout. To learn the GridBagLayout I created a very simple JPanel. When I test the JPanel the JButton and JTextField both appear in the center of the JPanel instead of at the left edge. How should I change my code to place the components at the left edge?

Below are two classes: the JPanel class and a tester.

Thanks for your help.

Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GbPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JButton btn_Choose = null;
	private JTextField tfDirectory = null;

	public GbPanel() {
		super();
		initialize();
	}

	private void initialize() {

		this.setSize(300, 200);
		this.setLayout(new GridBagLayout());

		// Constraints for button
		GridBagConstraints btnConstraints = new GridBagConstraints();
		btnConstraints.gridx = 0;
		btnConstraints.gridy = 0;
		btnConstraints.gridwidth = 1;
		btnConstraints.insets = new Insets(5,5,5,5);
		btnConstraints.anchor = GridBagConstraints.WEST;

		// Constraints for text field
		GridBagConstraints tfDirConstraints = new GridBagConstraints();
		tfDirConstraints.gridx = 0;
		tfDirConstraints.gridy = 1;
		tfDirConstraints.gridwidth = 3;
		tfDirConstraints.gridheight = 1;
		tfDirConstraints.insets = new Insets(5,5,5,5);
		tfDirConstraints.anchor = GridBagConstraints.WEST;

		this.add(getBtn_Choose(), btnConstraints);
		this.add(getTfDirectory(), tfDirConstraints);
	}

	private JButton getBtn_Choose() {
		if (btn_Choose == null) {
			btn_Choose = new JButton("Set ...");
		}
		return btn_Choose;
	}

	private JTextField getTfDirectory() {
		if (tfDirectory == null) {
			tfDirectory = new JTextField("<directory>");
		}
		return tfDirectory;
	}

}

Code:
import java.awt.BorderLayout;
import javax.swing.JFrame;

public class PanelTester
{
	private void doIt()
	{
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GbPanel p = new GbPanel();
		f.getContentPane().add(p);
		f.setBounds(0,0,300,100);
		f.setVisible(true);
	}
	public static void main(String args[])
	{
		PanelTester test = new PanelTester();
		test.doIt();
	}
}
 
Just add a weight to your button:

btnConstraints.weightx = 1;

Nitin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top