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);
}
}
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);
}
}