Solved

Problems with adding a JPanel to a container

Posted on 2002-04-06
1
204 Views
Last Modified: 2010-03-31
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
Comment
Question by:delphiusr
1 Comment
 
LVL 9

Accepted Solution

by:
yongsing earned 100 total points
ID: 6923492
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Groovy problem when using SOAPUI : DispatchException occurred 7 60
hibernate insert example 13 34
Error in @AspectJ Based AOP with Spring 2 18
Java string replace 11 55
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question