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!

GridBagLayout and multiple JPanel's on a single JFrame

843807Nov 8 2009 — edited Nov 9 2009
Hi!

I have a form (frame) with a few panels in JTabbedPane on it. Frame has a BorderLayout as tabbed pane will occupy whole form. But each panel should have a GridBagLayout as each will contain buttons, labels, text fields and text areas.
Here is the current code I use to set up layouts on all panels:
// adds a component to a panel
	private void addComponent(JPanel panel, Component component, int row, int column, int width, int height, GridBagLayout gbl, GridBagConstraints gbc)
		{
		gbc.gridx = column;
		gbc.gridy = row;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbl.setConstraints(component, gbc);
		panel.add(component);		
		}
	
	private void addPanel1Components()
		{
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		pSoft.setLayout(gbl);
		
		addComponent(Panel1, lblName, 0, 0, 1, 2, gbl, gbc);
                ...
                // It is followed by other 10+ similar calls for all of the components to be placed on this panel
		}
The question is should I create one global (for the current frame that is) GridBagLayout variable and use it with "new GridBagLayout()" operator for all panels or use the current approach?
As having a procedure with 8 parameters is kind of not good looking code and probably not the optimal one either.

NOTE: this question is also posted (but yet unanswered) here: [http://www.java-forums.org/awt-swing/22750-gridbaglayout-multiple-jpanels-single-jframe.html#post90045]
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 7 2009
Added on Nov 8 2009
8 comments
1,016 views