File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BoxLayout not working as advertised?

 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The BoxLayout API states:

Similarly, for a vertical layout, BoxLayout attempts to make all components in the column as wide as the widest component. If that fails, it aligns them horizontally according to their X alignments. For PAGE_AXIS layout, horizontal alignment is done based on the leading edge of the component. In other words, an X alignment value of 0.0 means the left edge of a component if the container's ComponentOrientation is left to right and it means the right edge of the component otherwise.


When I create a BoxLayout using Y_AXIS and add several components including a JLabel and JTextField the JLabel is centered. I have, however, invoked setAlignmentX(0.0F) on the JLabel which by my understanding should put it on the left. I thought the JLabel might be getting sized to fit and centering it's text, but when I add a line border to it I can see quite clearly that the label itself is centered. Why isn't it aligning the JLabel to the left?
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see now that the problem lies in the X alignment of the other components, nto the JLabel. Apparently if you add a few components with an X alignment of 0.0 and a few components with an X alignment of 0.5 the components with 0.0 still end up with their left most edge at the other's center point.

I don't understand the reasoning or logic behind this. If component A gets added to a JPanel with an alignment of 0.0 and component B gets added to a JPanel with an alignment of 0.5, shouldn't A be aligned to the left and B centered? This doesn't seem to be the case. What am I missing?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
works OK in this (java 1.4.0_01)

 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea why this doesn't work then? It will only achieve the desired affect if the each Box, JComboBox and JTextField has it's X alignment changed to 0.0F. I get the same results using JPanel's with a BoxLayout as I do with Box, so it's nothing unique to Box.



This is just a method to create a panel used in a much larger layout. I'm testing each panel individually as I make them to ensure they're at least behaving as expected individually. Any suggestions on improving the design are welcome.

P.S. They're final because they'll be used in an anonymous listener to validate the contents and update the backing when they lose focus or the user hits an appropriate key or button.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add these 4 lines and you'll get a bit closer,
but the horizontal ones are not right.

cityStatePanel.setAlignmentX(0.0f);
zipPhonePanel.setAlignmentX(0.0f);
birthGenderPanel.setAlignmentX(0.0f);
otherHealth.setAlignmentX(0.0f);
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already gotten what I want, I just don't understand why the above code didn't work. It required setting all of the components that weren't 0.0F already to be 0.0F. I don't understand why this is necessary.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've probably seen this (from Sun's boxlayout page)

"In general, all the components controlled by a top-to-bottom BoxLayout object
should have the same X alignment.
...
...
By default, most components have center X and Y alignment. However, buttons,
combo boxes, labels, and menu items have a different default X alignment
value: LEFT_ALIGNMENT."
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand Michael, the question I'm asking is why they all have to be the same. "Because Sun said so." isn't enough for me, personally, as I need to understand why this is the case in order to actually understand how the GUI is being built and not run into mysterious problems due to an ignorance of such things.
 
Craig Wood
Ranch Hand
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the 'Fixing Alignment Problems' section in the How to use BoxLayout lesson in the tutorial for some possibilities about why you were having trouble with your layout. When you run this app you'll see what was causing the trouble...
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig: I understand what was causing trouble. The problem is I don't understand why that causes trouble. I don't understand why they have to be the same, why one with 0.5 and one with 0.0 leads to almost the exact opposite result as I would logically expect. People keep telling me what is wrong with it but nobody seems to be able to answer why.
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, can anyone tell me what would happen if the JLabel had an X alignment of 0.3 and a JTextField of 0.5 and a JComboBox of 0.7 and they were all put in a vertical Box? If you can't answer that question without coding it and looking to see what happened I'm guessing you don't know how it works either. That's what I want to know!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic