• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Problems with adding a JPanel to a container

Hi!, I placed this piece of code in a function called componentsLayout which is called by the init() function of my applet (which by the way inherits JApplet). I can't figure out why it won't follow my gridbaglayout settings.

I wanted to produce this effect:

|--------------------|
|button | button 2   |
|       -------------|  
|   1   | button 3   |
---------------------

However, the output of my code is this:

|--------------------------------|
|button | button 2   | button 3  |
|--------------------------------|  

here's the code snippet:

Container container = getContentPane();
GridBagLayout gbLayout = new GridBagLayout();
container.setLayout(gbLayout);
         
GridBagConstraints c = new GridBagConstraints();

c.gridx = 0;
c.gridy = 0;
c.gridheight = 2;
c.weighty = 1;

c.fill = GridBagConstraints.BOTH;
         
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");

JPanel panel = new JPanel();
panel.setSize(400,400);
         
panel.add(button1,c);
                   
c.gridx = 1;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.gridheight = 2;

panel.add(button2,c);
                   
c.gridx = 1;
c.gridy = 1;
c.weightx = 1;
c.weighty = 1;
c.gridheight = 2;

panel.add(button3,c);

c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.weightx =1;
c.gridheight = 2;
c.weighty = 1;
         
gbLayout.setConstraints(panel,c);
container.add(panel);

The reason why I used this rather strange setting of creating a panel first instead of adding the buttons directly is because I wanted to try a simpler code of adding buttons to a panel before I proceed to debug my project. Its even more strange that when I tried to directly set the buttons without putting it on the panel, it produced the correct effect.

Btw, is there an IDE that I can use to trace step by step the applet code? my source code contains roughly about 5000 lines.
 
Please help. Thanks in advance!

0
delphiusr
Asked:
delphiusr
1 Solution
 
yongsingCommented:
The reason that it is not working is because you did not set the layout for your JPanel. A JPanel has a default layout of FlowLayout, and that's why you're getting a flow layout, as expected. You only set the layout for the content pane, which contains your JPanel. However, the buttons are contained in the JPanel, so you need to set the layout on the JPanel. Also, I noticed that you did not set the constraints for each button before you add them to the JPanel.

Container container = getContentPane();
GridBagLayout gbLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");

JPanel panel = new JPanel(gbLayout); // create a JPanel with GridBagLayout
panel.setSize(400,400);

c.gridheight = 2; // button1 spans across two rows
c.fill = GridBagConstraints.BOTH;
gbLayout.setConstraints(buttonl, c) // set the constaints for button1
panel.add(button1,c); // add button1 to JPanel
                                       
c.gridheight = 1; // button2 spans across one row only
c.gridwidth = GridBagLayout.REMAINDER;
gbLayout.setConstraints(button2, c) // set the constaints for button2
panel.add(button2,c); // add button2 to JPanel

gbLayout.setConstraints(button3, c) // set the constaints for button3                                        
panel.add(button3,c); // add button3 to JPanel

container.add(panel); // add your JPanel to the content pane

Since the content pane has a border layout, your JPanel will be added to the center of the content pane. If you want the buttons to fill up the content pane completely, then add them directly to the content pane instead:

GridBagLayout gbLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
Container container = getContentPane();
container.setLayout(gbLayout); // set GridBagLayout to content pane

JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");

c.gridheight = 2; // button1 spans across two rows
c.fill = GridBagConstraints.BOTH;
gbLayout.setConstraints(buttonl, c) // set the constaints for button1
container.add(button1,c); // add button1 to content pane
                                       
c.gridheight = 1; // button2 spans across one row only
c.gridwidth = GridBagLayout.REMAINDER;
gbLayout.setConstraints(button2, c) // set the constaints for button2
container.add(button2,c); // add button2 to content pane

gbLayout.setConstraints(button3, c) // set the constaints for button3                                        
container.add(button3,c); // add button3 to content pane

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now