Solved

Problems with adding a JPanel to a container

Posted on 2002-04-06
1
201 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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now