This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, my first newbie question. I did search and could not find an answer elsewhere. I am creating a small application at work as part of a learning experience in Java, a simple How-To manual for our newbies. At this point I am trying to get the banner to look right, and have made some progress. Where I am currently stuck is in spacing the lines, I need to get them close together than the app is currently doing. The lines in question are in the banner section; I need to cut the spacing by half, while leaving the font sizes as they are.
Looks like you have the components compressed as much as possible in the vertical dimension in your GridLayout. To reduce the space further you could try two things. One is to reduce the preferredSize.height of the labels. Another is to use different layouts. One of the difficulties with using GridLayout is that it doesn't pay much attention to the preferredSize of each of the components but tries to divide the available space into a grid of equal size cells. Using a layout manager that respects the preferredSize of its (containers) child components can help reduce the space used for display. FlowLayout and GridBagLayout are two layout managers that try to show every child component at its preferredSize. Testing indicates that the first option might be more effective than the second.
Originally posted by Mike Lipay: Where I am currently stuck is in spacing the lines, I need to get them close together than the app is currently doing. The lines in question are in the banner section; I need to cut the spacing by half, while leaving the font sizes as they are.
I'm not sure exactly what your code is doing when it uses the same layoutTF instance in multiple containers but tries to setVgap() differently for each. I don't think that has anything to do with your spacing problem, though.
One problem is that your row1 and row2 panels default to having a 5-pixel border. You can get rid of this by replacing new JPanel() with new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0).
Another problem is just that JLabels return a preferred size that is larger than you want. You can easily set a preferred size that is shorter. Here's some code demonstrating way to do this. The shorter preferred size is set in the squish() method. (This code doesn't use row1/row2, so their 5-pixel borders don't come into play.)
This code also changes your setHorizontalAlignment(0) calls to setHorizontalAlignment(JLabel.CENTER). It works out the same but makes it easier to understand what it is doing.
[edit: Mention that code doesn't use row1/row2.] [ September 12, 2007: Message edited by: Brian Cole ]
Thanks. I tried changing the preferredSize, but nothing happened. Flow layout doesn't work well because it fits more on a line than I really want there, the desired layout is very specific and more important than the distance between the lines. Just would have been nice to give them exactly what they wanted on my first project.