Solved

Problems with adding a JPanel to a container

Posted on 2002-04-06
1
206 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaFX TableView not displaying correctly 3 150
Java Restore security prompts not working 10 241
Chrome and Firefox Java 5 88
jmss example java 2 47
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

737 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