Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2005

Tip: Looking for answers? Try searching our database.

GridLayout and glue

Thread view: 
Florence HENRY - 25 Jan 2005 13:30 GMT
Hello,

When I put a JPanel with a GridLayout, followed by a
Box.createVerticalGlue, why does not all the extra space go into the
glue? It seems that the extra space is spread within the gridLayout and
the glue.

With the following code, there is plenty of space inside the JPanel
mainPanel. How can I force all the extra space to go in the glue ?

Thanks.

import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;

public class MyApplet extends JApplet  {
   
   public void init() {  
       try {
           javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
               public void run() {
                   createGUI();
               }
           });
       } catch (Exception e) {}
   }
   
   private void createGUI() {
       JPanel mainPanel = new JPanel();
       mainPanel.setLayout(new GridLayout(2,2));
       
       JPanel panel = new JPanel();
       panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
       panel.setBorder(BorderFactory.createTitledBorder("panel"));

       JButton button1 = new JButton("button 1");
       panel.add(button1);
       
       mainPanel.add(panel);
       
       JPanel otherpanel = new JPanel();
       otherpanel.setLayout(new BoxLayout(otherpanel,
BoxLayout.Y_AXIS));
       otherpanel.setBorder(BorderFactory.createTitledBorder("other
panel"));

       JButton button2 = new JButton("button 2");
       otherpanel.add(button2);

       mainPanel.add(otherpanel);

       JButton button3 = new JButton("button 3");
       mainPanel.add(button3);
       JButton button4 = new JButton("button 4");
       mainPanel.add(button4);

       getContentPane().setLayout(new BoxLayout(getContentPane(),
BoxLayout.Y_AXIS));
       getContentPane().add(mainPanel);
       getContentPane().add(Box.createVerticalGlue());
   }
   
   public void start() {}
   
   public void stop() {}
   
   public void destroy() {}
   
}

Signature

Florence Henry
florence point henry arobasse obspm point fr

A. Bolmarcich - 25 Jan 2005 17:27 GMT
> Hello,
>
> When I put a JPanel with a GridLayout, followed by a
> Box.createVerticalGlue, why does not all the extra space go into the
> glue? It seems that the extra space is spread within the gridLayout and
> the glue.
[snip useful example program, although examples that use a JFrame are
easier to run than examples that use a JApplet]

That is what BoxLayout does.  It distributes extra space to its
components in the direction that the components are laidout up to the
maximum size of each component it that direction.  Each of the two
components in the box has the default maximum height of Short.MAX_VALUE.

If you want the container using GridLayout to not get any extra space,
instead of using BoxLayout for the ContentPane of the JApplet use
BorderLayout and add the container with the grid to the ContentPane
with a layout constraint of BorderLayout.NORTH.
Florence HENRY - 26 Jan 2005 08:19 GMT
> [snip useful example program, although examples that use a JFrame are
> easier to run than examples that use a JApplet]

Sorry... I will do better next time !

> That is what BoxLayout does.  It distributes extra space to its
> components in the direction that the components are laidout up to the
> maximum size of each component it that direction.  Each of the two
> components in the box has the default maximum height of Short.MAX_VALUE.

Strange... I did not understood it that way before.... When I put a
BoxLayout container, followed by a vertical glue, in another BoxLayout
container, the extra space is not distributed.

It is a special behavior between BoxLayout and Gridlayout ?

> If you want the container using GridLayout to not get any extra space,
> instead of using BoxLayout for the ContentPane of the JApplet use
> BorderLayout and add the container with the grid to the ContentPane
> with a layout constraint of BorderLayout.NORTH.

Thanks. It works nice now.

Signature

Florence Henry
florence point henry arobasse obspm point fr

A. Bolmarcich - 26 Jan 2005 16:04 GMT
> Strange... I did not understood it that way before.... When I put a
> BoxLayout container, followed by a vertical glue, in another BoxLayout
> container, the extra space is not distributed.
>
> It is a special behavior between BoxLayout and Gridlayout ?

It is a difference in the maximum sizes of containers using BoxLayout
and GridLayout.  The maximum size of a container using BoxLayout is the
same as the preferred size of the container (the value is returned by
the maximumLayoutSize() method of BoxLayout).  The maximum size of a
container using GridLayout is Dimension(Short.MAX_VALUE,Short.MAX_VALUE)
(the value is returned by the getMaximumSize() method of
java.awt.Component).

When an enclosing container uses BoxLayout, the size of each component
may be increased up to the component's maximum size in the box layout
direction.  An enclosed container that uses BoxLayout as its
layout manager will not have its size increased because its maximum
size is the same as its preferred size.  An enclosed container that
uses GridLayout will have its size increased up to Short.MAX_VALUE.
Florence HENRY - 27 Jan 2005 18:20 GMT
> It is a difference in the maximum sizes of containers using BoxLayout
> and GridLayout.  The maximum size of a container using BoxLayout is the
[quoted text clipped - 10 lines]
> size is the same as its preferred size.  An enclosed container that
> uses GridLayout will have its size increased up to Short.MAX_VALUE.

Thanks ! I understand better now.

Signature

Florence Henry
florence point henry arobasse obspm point fr



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.