Using BoxLayout rather than GridBagLayout
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Philip_Miller
Posted On:   Thursday, September 6, 2001 06:36 AM

In John Zukowski's book, Definitive Guide to Swing, Figure 10-2, page 372 shows a picture of a container with a 2-d grid of buttons that uses a BoxLayout rather than a GridBagLayout. I would like to see BoxLayout sample code that creates this kind of layout. In that figure there is a 3x3 grid of seven buttons. Button "four" spans two columns and button "five" spans two rows. Despite the irregular sizing of these two buttons, all the buttons are aligned on grid boundaries and when the container is resized, all new space goes to buttons "four" and "five". How can I use a Box or BoxLayout to achieve the results in that fi   More>>

In John Zukowski's book, Definitive Guide to Swing, Figure 10-2,
page 372 shows a picture of a container with a 2-d grid of buttons
that uses a BoxLayout rather than a GridBagLayout. I would like
to see BoxLayout sample code that creates this kind of layout.


In that figure there is a 3x3 grid of seven buttons. Button "four"
spans two columns and button "five" spans two rows. Despite the
irregular sizing of these two buttons, all the buttons are aligned
on grid boundaries and when the container is resized, all new
space goes to buttons "four" and "five".


How can I use a Box or BoxLayout to achieve the
results in that figure where the different sized buttons are all aligned
in both horizontal and vertical directions even though "four" spans
two columns and "five" spans two row?


The figure shows


			


-------------------------
|one | two | three |
-------------------------
| four | |
----------------| five |
| six | seven | |
-------------------------



Thanks,
Phil
pwmiller@sarnoff.com    <<Less

Re: Using BoxLayout rather than GridBagLayout

Posted By:   Scott_Stanchfield  
Posted On:   Friday, September 28, 2001 07:19 PM

The "trick" is to get the buttons to agree on their preferred size. You can do that using the following code. (There's a little bug in there somewhere -- "four" doesn't fill it's width, but I don't have time to hunt it down)

SameSizeGroup -- Coordinates the sizes


package com.javadude.gui.samesize;

import java.awt.Dimension;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


public class SameSizeGroup {
private Set elements = new HashSet();

public void add(SameSizeDecorator d) {
elements.add(d);
d.setSameSizeGroup(this);
}
public void remove(SameSizeDecorator d) {
elements.remove(d);
d.setSameSizeGroup(null);
}

public Dimension getPreferredSize() {
Dimension size = new Dimension(0,0);
Iterator i = elements.iterator();
while(i.hasNext()) {
SameSizeDecorator d = (SameSizeDecorator)i.next();
Dimension dSize = d.getRealPreferredSize();
size.width = Math.max(size.width, dSize.width);
size.height = Math.max(size.height, dSize.height);
}
return size;
}
}


SameSizeDecorator -- Adapts the size of its contents to its SameSizeGroup


package com.javadude.gui.samesize;


import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;


public class SameSizeDecorator extends Container {
private SameSizeGroup sameSizeGroup;

/**
* Constructor for SameSizeDecorator
*/
public SameSizeDecorator(Component c) {
setLayout(new GridLayout(1,0));
add(c);
}


public Dimension getPreferredSize() {
if (getSameSizeGroup() != null)
return getSameSizeGroup().getPreferredSize();
else
return getRealPreferredSize();
}


public Dimension getRealPreferredSize() {
return super.getPreferredSize();
}

/**
* Gets the sameSizeGroup
* @return Returns a SameSizeGroup
*/
public SameSizeGroup getSameSizeGroup() {
return sameSizeGroup;
}
/**
* Sets the sameSizeGroup
* @param sameSizeGroup The sameSizeGroup to set
*/
public void setSameSizeGroup(SameSizeGroup sameSizeGroup) {
this.sameSizeGroup = sameSizeGroup;
}
}


Test -- A same test that does the example GUI you presented (with a minor bug for button four -- need to look at this...)


package com.javadude.gui.samesize;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class Test {
private static final Dimension MAX = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
private static final Dimension MIN = new Dimension(0,0);

public static void main(String[] args) {
JButton b1 = new JButton("one");
JButton b2 = new JButton("two");
JButton b3 = new JButton("three");
JButton b4 = new JButton("four");
JButton b5 = new JButton("five five five five");
JButton b6 = new JButton("six six six six");
JButton b7 = new JButton("seven");

b2.setMaximumSize(MAX);
b2.setMinimumSize(MIN);
b4.setMaximumSize(MAX);
b4.setMinimumSize(MIN);
b7.setMaximumSize(MAX);
b7.setMinimumSize(MIN);

SameSizeDecorator s1 = new SameSizeDecorator(b1);
SameSizeDecorator s6 = new SameSizeDecorator(b6);
SameSizeGroup col1 = new SameSizeGroup();
col1.add(s1);
col1.add(s6);

SameSizeDecorator s3 = new SameSizeDecorator(b3);
SameSizeDecorator s5 = new SameSizeDecorator(b5);
SameSizeGroup col3 = new SameSizeGroup();
col3.add(s3);
col3.add(s5);

JFrame f = new JFrame();
f.getContentPane().setLayout(new BoxLayout(f.getContentPane(),BoxLayout.Y_AXIS));
JPanel top = new JPanel();
top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
top.add(s1);
top.add(b2);
top.add(s3);

JPanel bottom = new JPanel();
bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));

JPanel bottomLeft = new JPanel();
bottomLeft.setLayout(new BoxLayout(bottomLeft, BoxLayout.Y_AXIS));
bottomLeft.add(b4);

JPanel bottomLeftBottom = new JPanel();
bottomLeftBottom.setLayout(new BoxLayout(bottomLeftBottom, BoxLayout.X_AXIS));
bottomLeftBottom.add(s6);
bottomLeftBottom.add(b7);
bottomLeft.add(bottomLeftBottom);

bottom.add(bottomLeft);
bottom.add(s5);

f.getContentPane().add(top);
f.getContentPane().add(bottom);
f.pack();
f.setVisible(true);

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
}
}
About | Sitemap | Contact