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?
 
CEHJCommented:
Firstly, don't call your class 'GridBagLayout' - that's asking for trouble as there's already an API class of that name
0
 
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.