Strange JFrame border
Hi everybody,
This isn't technically a problem, just something weird going on with my JFrame that I don't quite understand. My JFrame has a JLabel added to the north panel, and a JPanel with 3 JButtons in a 3-column GridLayout added to the south panel. When the frame renders, the little gray default border around the JFrame is the same width all the way around, except for on the right side of the button panel, where it's a little wider, so it looks like the JLabel in the top panel is longer than the button panel. It's not really a big deal, but I was just curious, what could cause this to happen? I've never seen anything like this before.
Any explanation would be great, and thanks!
Jezzica85
[730 byte] By [
jezzica85a] at [2007-11-27 11:20:31]

# 1
are you sure it's not a combination of the button's border and frame's border
making it appear a little wider.
add this, see if there's a difference
button3.setBorder(null);
# 2
> I've never seen anything like this before.
Me either.
You know my standard reply!
# 3
Ah, yes, the good old self-contained program. I was hoping we wouldn't need one this time, but I guess I should just make a habit of it.
Anyway, here it is:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Test {
public static void main(String args[]) {
try {
JFrame frame = new JFrame( "Test" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLayout( new BorderLayout() );
frame.setSize( 700, 500 );
// Create main label
JLabel label = new JLabel( " Test " );
label.setBackground( Color.black );
label.setForeground( Color.lightGray );
label.setHorizontalAlignment( SwingConstants.CENTER );
label.setOpaque( true );
frame.add( label, BorderLayout.NORTH );
// Create button panel and buttons
JPanel panel = new JPanel( new GridLayout( 0, 3 ) );
for( int i = 0; i < 3; i++ ) {
JButton button;
if( i == 0 ) {
button = new JButton( "I" );
} else if( i == 1 ) {
button = new JButton( "II" );
} else {
button = new JButton( "III" );
}
button.setOpaque(true);
button.setBackground(Color.pink);
panel.add( button );
}
frame.add( panel );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
} catch( Exception e ) { e.printStackTrace(); }
}
}
I made the label and buttons colored so you can see the right border more clearly and see what the problem is.
Thanks!
Jezzica85
# 4
Its a combination of two things:
a) as Michael suggested you have the button border on the right side. Change your example to use JLabels since they don't use Borders
b) the size of the panel is not divisible by 3 so when the component is painted a pixel or two from the parent is still visible. A grid resizes all components to be the same size. The last component does not get the "extra" space if the area is not divisable by the number of components in the grid.
# 5
due to the width of the frame not being a multiple of 3.
if you (very) slowly drag the frame wider, you will see the gap narrow,
then disappear, then reappear, narrow, disappear reappear etc
# 6
Thanks camickr and Michael,
I understand the panel not being divisible by 3, but I don't understand why I should change the JButtons to Labels. Don't we need JButtons if we need to use ActionListeners?
EDIT: I did try the idea about making the panel width divisible by 3, but it only seemed to work if I used a width that was divisible by 3, minus 1, whether I had borders on the buttons or not. Does that mean I'm still missing something?
Jezzica85
Message was edited by:
jezzica85
Message was edited by:
jezzica85
# 7
might be easier if you set a preferredsize for one of the buttons and, instead of
frame.setSize( 700, 500 );
use
frame.pack();
but after you add the components e.g.
frame.add( panel );
frame.pack();//<-
frame.setLocationRelativeTo( null );
# 8
Thanks Michael,
I kind of need you to clarify, though, what would setting preferred size of one button do? Wouldn't that make the other buttons different sizes once I called pack()?
# 9
gridlayout sizes all components the same - they will all be as wide as the widest,
and as tall as the tallest, so you only need to specify a size for one of them.
try it - see what happens.
# 10
Hmm...when I set the preferred size of one component and use pack, the panel doesn't show up. Is that normal?
# 11
that is normal if you called pack() before adding components.
re-read reply #7
# 12
Well, that was weird. I could have sworn I added everything before pack(). I guess I didn't. I got it working now, anyway. Thank you!
Jezzica85