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();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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();
}
}
ASKER
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();
}
}
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();
}
}
ASKER
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.
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();
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
BorderLayout andothe with FloweLaout - it can do reaclly complex things wuitghout complexity of GridbagLayout
ASKER
for_van I understand why? That I needed to know. I will now close off this session?
:)
ASKER