Trouble managing size of Swing components

N

news.rcn.com

I'm having trouble controlling the size of components in Swing apps
using different layout managers.

Can anyone recommend a summary that explains what dimensions and sizes
(max, preferred, min) are respected by different managers and under
what conditions. I find it hard to get a good overview from Sun's
online docs and tutorials. It seems this information might be
summarized in a table somewhere.

In the simple example below a button is created with a preferred size.
When the window is first displayed, the button is small and there is
blank space under the stack of buttons. If the window is then
shortened, the sized button increases in size and the button stack
fills the window vertically. Further shortening of the window
shortens the sized button, keeping the window full vertically.

My expectation is that the sized button should stay its preferred size
during these manipulations.

Can anyone tell me what I'm missing here?

Thanks in advance.



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class SizeQuestion{

static JButton sizedButton = null;

public static void main(String[] args)
{
// Create a button with a preferred size, add a border,
// Have it write its size to stdout
sizedButton = new JButton( "This button is sized" );
sizedButton.setPreferredSize( new Dimension( 200, 75 ) );
sizedButton.setBorder( new LineBorder( Color.RED, 5 ) );
sizedButton.addComponentListener( new ComponentAdapter()
{
public void componentResized( ComponentEvent evt )
{
System.out.println( "Resized: " + sizedButton.getSize() );
}
} );


// Make a panel with vertical box layout; add our sized
// button between two others.
JPanel aPanel = new JPanel();
aPanel.setLayout(new BoxLayout( aPanel, BoxLayout.Y_AXIS));
aPanel.setBackground( Color.CYAN );

aPanel.add( new JButton( "A button" ) );
aPanel.add( sizedButton );
aPanel.add( new JButton( "Another button" ) );



// Create a man window, add our panel, and show it.
JFrame frame = new JFrame("SizeQuestion");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add( aPanel );

frame.pack();
frame.setVisible(true);
}
}
 
A

andrewh1

news.rcn.com said:
Can anyone tell me what I'm missing here?

What you are missing are the maximum and minimum sizes.

As the layout manager tries to layout the container it looks at these
values as well as the preferred size in a particular order.
If you haven't set these values then the component supplies a value
based upon the text in the component. In your case the minimum and
maximum heights are both set to the default values which are defined by
the text in the button. In this case both are set to 26 pixels.

When the layout manager does its thing it looks at each component in
turn and tries to fit it into the container using the minimum sizes. If
there is excess space then it apportions the space based upon the
preferred size. If there is still excess space it starts again using
the maximum sizes. So while the buttons can fill the full panel height
using up to a maximum of 75 pixels for the sizedButton it will do so.
As soon as the panel expands so that there is excess space when the
sizedButton is set at 75 pixels, it is set to 26 pixels.

Not exactly behaviour one would expect in this situation. Perhaps the
maximum should be set to the Math.max() of the text or the preferred
size. However perhaps the author wrote it this way to get the desired
action in a different (and possibly more common) scenario.

You can get the action you require by simply adding the line

sizedButton.setMaximumSize( new Dimension( 200, 75 ) );

when you are specifying the sizedButton. The button then expands as
possible until it reaches 75 pixels high and then stays at that size.


Cheers

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top