Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problems with adding a JPanel to a container

Posted on 2002-04-06
1
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 9

Accepted Solution

by:
yongsing earned 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

636 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