How to solve a setlayout error

I am getting an error in line 42 - " The method setLayout(LayoutManager) in the type Container is not applicable for the arguments (GridBagLayout)"  Please help me to resolve this error.
package formslayout;

import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GridBagLayout {
	
  public static void main(String[] args) {

	  JFrame f = new JFrame();
	  JPanel p = new JPanel();
//	  GridBagLayout gl = new GridBagLayout();
	  p.setLayout(new GridBagLayout());

    // Place a component at cell location (1,1)
	  JTextField customer = new JTextField();
	  JTextField address = new JTextField();
	  
	  GridBagConstraints c = new GridBagConstraints();
	  c.insets = new Insets(2, 2, 2, 2); // insets for all components
	  c.gridx = 1;  // column
	  c.gridy = 1;	// row
	  p.add(customer, c); // constraints passed in
	  c.gridx = 1;
	  c.gridy = 2;
	  p.add(address, c); // constraints passed in
	   
	  f.add(p);
	  f.setSize(600, 200);
	  f.setVisible(true);
	  f.pack();
  }
}

Open in new window

Vanavah EdwardsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Firstly, don't call your class 'GridBagLayout' - that's asking for trouble as there's already an API class of that name
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vanavah EdwardsAuthor Commented:
Mistake the error is in line 17.
0
Vanavah EdwardsAuthor Commented:
Okay I am changing it now.  I should have know better about reserved words.
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

CEHJCommented:
e.g.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GBagLayout {
	
  public static void main(String[] args) {

	  JFrame f = new JFrame();
	  JPanel p = new JPanel();
//	  GridBagLayout gl = new GridBagLayout();
	  p.setLayout(new GridBagLayout());

    // Place a component at cell location (1,1)
	  JTextField customer = new JTextField(10);
	  JTextField address = new JTextField(10);
	  
	  GridBagConstraints c = new GridBagConstraints();
	  c.insets = new Insets(2, 2, 2, 2); // insets for all components
	  c.gridx = 1;  // column
	  c.gridy = 1;	// row
	  p.add(customer, c); // constraints passed in
	  c.gridx = 1;
	  c.gridy = 2;
	  p.add(address, c); // constraints passed in
	   
	  f.add(p);
	  f.setVisible(true);
	  f.pack();
  }
}

Open in new window

0
Vanavah EdwardsAuthor Commented:
I renamed it to GridBagLayoutDemo.  I am still getting the same error.
0
for_yanCommented:
This works without any error:

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GridBagLayoutDemo {

  public static void main(String[] args) {

	  JFrame f = new JFrame();
	  JPanel p = new JPanel();
//	  GridBagLayout gl = new GridBagLayout();
	  p.setLayout(new GridBagLayout());

    // Place a component at cell location (1,1)
	  JTextField customer = new JTextField();
	  JTextField address = new JTextField();

	  GridBagConstraints c = new GridBagConstraints();
	  c.insets = new Insets(2, 2, 2, 2); // insets for all components
	  c.gridx = 1;  // column
	  c.gridy = 1;	// row
	  p.add(customer, c); // constraints passed in
	  c.gridx = 1;
	  c.gridy = 2;
	  p.add(address, c); // constraints passed in

	  f.add(p);
	  f.setSize(600, 200);
	  f.setVisible(true);
	  f.pack();
  }
}

Open in new window

0
for_yanCommented:
AAnd this even draws decent textboxes - you don't need GridBagLauyout for that though

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GridBagLayoutDemo {

  public static void main(String[] args) {

	  JFrame f = new JFrame();
	  JPanel p = new JPanel();
//	  GridBagLayout gl = new GridBagLayout();
	  p.setLayout(new GridBagLayout());

    // Place a component at cell location (1,1)
	  JTextField customer = new JTextField(15);
	  JTextField address = new JTextField(15);

	  GridBagConstraints c = new GridBagConstraints();
	  c.insets = new Insets(2, 2, 2, 2); // insets for all components
	  c.gridx = 1;  // column
	  c.gridy = 1;	// row
      c.weightx = 10;
        c.weighty = 10;

	  p.add(customer, c); // constraints passed in
	  c.gridx = 1;
	  c.gridy = 2;
      c.weightx = 10;
        c.weighty = 10;
	  p.add(address, c); // constraints passed in

	  f.add(p);
	  f.setSize(600, 200);
	  f.setVisible(true);
	  f.pack();
  }
}

Open in new window

0
Vanavah EdwardsAuthor Commented:
The error has disappeared.  Other than the class name, Is the difference the GridBagLayout import?  And why?
0
Mike EghtebasDatabase and Application DeveloperCommented:
vanavah,

I tested it too. It works for me. I named it GBagLayoutTest.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GBagLayoutTest {
	
  public static void main(String[] args) {

	  JFrame f = new JFrame();
	  JPanel p = new JPanel();
//	  GridBagLayout gl = new GridBagLayout();
	  p.setLayout(new GridBagLayout());

    // Place a component at cell location (1,1)
	  JTextField customer = new JTextField(10);
	  JTextField address = new JTextField(10);
	  
	  GridBagConstraints c = new GridBagConstraints();
	  c.insets = new Insets(2, 2, 2, 2); // insets for all components
	  c.gridx = 1;  // column
	  c.gridy = 1;	// row
	  p.add(customer, c); // constraints passed in
	  c.gridx = 1;
	  c.gridy = 2;
	  p.add(address, c); // constraints passed in
	   
	  f.add(p);
	  f.setVisible(true);
	  f.pack();
  }
}

Open in new window

0
for_yanCommented:
Yes, when you renamed it , IDE offered to import awt.*
I belieeve and so it was frst point tin to your class
now New GridBagLayou refers to standard ckass
0
Vanavah EdwardsAuthor Commented:
I know for_van but I did that to learn it because the other layout managers I have studied in detailed namely border, card, box, and grid cannot do complex screen.  They only place the components equally in rows and columns.
0
CEHJCommented:
>>I renamed it to GridBagLayoutDemo.  I am still getting the same error.

See the code i posted
0
for_yanCommented:
But you can make panels ansd spilit your screen wfirst with pabnels - nthen one of them cvan be layour with grid andother with
BorderLayout andothe with FloweLaout - it can do reaclly complex things wuitghout complexity of GridbagLayout
0
Vanavah EdwardsAuthor Commented:
for_van I understand why?  That I needed to know.  I will now close off this session?
0
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.