Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Facing problems with Panels

Gaurav Chaturvedi
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am new to Swings so this problem might seem very stupid to you but i have tried to solve it a lot but culdnot. acctually i am trying to fit two panels on top of another is it possible.the base panel has a Gridbag layout with the help of which i have created wto rows the first row would contain another panel with Gridbag layout and the second row would be on a border layour format . i am posting the code too tell me the error please

import java.awt.*;
import javax.swing.*;
public class ThreePanels_Panel1 extends JFrame
JPanel pan;
GridBagLayout gridBag;
GridBagConstraints gridConstraint;

public ThreePanels_Panel1()
super ();
pan = new JPanel ();
gridBag = new GridBagLayout ();
gridConstraint = new GridBagConstraints ();

gridConstraint.gridx = 1;
gridConstraint.gridy = 1;
ThreePanels_Panel2 panel2 = new ThreePanels_Panel2 ();
gridBag.setConstraints (panel2,gridConstraint);

gridConstraint.gridx = 1;
gridConstraint.gridy = 2;
ThreePanels_Panel3 panel3 = new ThreePanels_Panel3 ();
gridBag.setConstraints (panel3,gridConstraint);

pack ();

public static void main (String[] args)
new ThreePanels_Panel1();

class ThreePanels_Panel2
JPanel pan_Panel2;
GridBagLayout lout_Grid;
GridBagConstraints const_GridBag;
//BorderLayout lout_Border;
JLabel lbl_Name,lbl_City,lbl_Gender,lbl_Image,lbl_Blank;
JTextField txtfld_Name,txtfld_City;
JRadioButton bty_Male,bty_Female;
JButton bty_Image;
ButtonGroup bty_Grp;
Icon icon_Image;

public ThreePanels_Panel2 ()
pan_Panel2 = new JPanel();
lout_Grid = new GridBagLayout();
const_GridBag = new GridBagConstraints();
//lout_Border = new BorderLayout();
bty_Grp = new ButtonGroup ();
bty_Male = new JRadioButton("Male");
bty_Female = new JRadioButton("Female");

const_GridBag.gridx = 1;
const_GridBag.gridy =1;
lbl_Blank = new JLabel ("");

const_GridBag.gridx = 1;
const_GridBag.gridy =2;
lbl_Name = new JLabel ("Name");

const_GridBag.gridx = 2;
txtfld_Name = new JTextField (25);

const_GridBag.gridx = 1;
const_GridBag.gridy =3;
lbl_City = new JLabel ("City");

const_GridBag.gridx = 2;
txtfld_City = new JTextField (25);

const_GridBag.gridx = 1;
const_GridBag.gridy =4;
lbl_Gender = new JLabel ("Gender");

const_GridBag.gridx = 2;
const_GridBag.gridy =4;

const_GridBag.gridx = 3;
const_GridBag.gridy =4;

const_GridBag.gridx = 4;
const_GridBag.gridy =1;
const_GridBag.gridheight =50;
icon_Image = new ImageIcon ("21072006543.jpg");
lbl_Image = new JLabel (icon_Image);



class ThreePanels_Panel3
JPanel panel;
JButton bty_First,bty_Next,bty_Last,bty_Submit,bty_Reset;
BorderLayout lyout_Border;
public ThreePanels_Panel3()
panel=new JPanel();
lyout_Border=new BorderLayout();
bty_First=new JButton("First");
bty_Next=new JButton("Next");
bty_Last=new JButton("Last");
bty_Submit=new JButton("Submit");
bty_Reset=new JButton("Reset");
Campbell Ritchie
Posts: 50699
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags, with the button below the "message" box, otherwise you end up with a monolithic block of code which is very hard to read.
Various points at a quick look at your code.
Why are you starting with gridx = 1, gridy = 1, rather than 0, 0?
What is class ThreePanel2 supposed to extend? You appear not to be adding JPanels, but instances of the ThreePanels classes. Have you used ctrl-c ctrl-v to post your code?
Even if those classes do extend JPanel, I don't think you can add two components to the same location in a layout. You will have to go through the Java Tutorials about layouts and check. If you want two panels in the same location ( ) I think you have to add your 1st panel to the JFrame, then add the 2nd panel to the 1st panel.

Have you come across Horstmann's GBC class? It makes using GridBag much easier. If you are using a sophisticated layout like GridBag (or SpringLayout or GroupLayout) you might as well add everything to the top-level JFrame, rather than using multiple panels.
pete stein
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that it's a common mistake to think that complex layout problems are only solvable by complex layout managers such as GridBagLayout. Don't fall into this trap. In most situations you are far better off using simpler layout managers in JPanels that are nested one in the other and reserving GridBagLayout for specific situations. Get to know the Layout managers via the Sun tutorials as recommended above. They will help you immensely.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic