Link to home
Start Free TrialLog in
Avatar of Vanavah Edwards
Vanavah Edwards

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vanavah Edwards
Vanavah Edwards

ASKER

Mistake the error is in line 17.
Okay I am changing it now.  I should have know better about reserved words.
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

I renamed it to GridBagLayoutDemo.  I am still getting the same error.
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

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

The error has disappeared.  Other than the class name, Is the difference the GridBagLayout import?  And why?
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
>>I renamed it to GridBagLayoutDemo.  I am still getting the same error.

See the code i posted
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
for_van I understand why?  That I needed to know.  I will now close off this session?
:)