aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Swing - valign Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Swing - valign" Watch "Swing - valign" New topic
Author

Swing - valign

Anna Hays
Ranch Hand

Joined: Nov 09, 2003
Posts: 131
Hi, I have problems with swing again! It hates me and I hate it too...

I have 3 items in a FlowLayout. But they seem to be middle valigned. How do I make them align to the top???

Thank you!!
Greg Till
Greenhorn

Joined: May 10, 2004
Posts: 5
Hi Anna,

It's a little difficult to picture your problem without seeing your code, but the way I make things vertically aligned to the top is to set the Layout manager for your container to BorderLayout, add a new container to it in the NORTH position and then add your components to this new container.

So if you were adding a button to a JFrame -



Hope this helps,

Greg

PS swing is tricky to get the hang of, but it is a beautiful thing once you get the hang of it!
[ May 19, 2004: Message edited by: Greg Till ]
Anna Hays
Ranch Hand

Joined: Nov 09, 2003
Posts: 131
I wanna valign 3 components to the top, top left, top middle and top right, not just one component. ;( How do I do that? Sorry it is not clear, got a large amount of code, thot it won't look good here.


PS swing is tricky to get the hang of, but it is a beautiful thing once you get the hang of it!

I hope that is true so much. I feel it is bit easier now, but far from beautiful...I want to do the GUI in Flash!!!
Greg Till
Greenhorn

Joined: May 10, 2004
Posts: 5
OK, I think I can see what you are trying to do (had to enclose this not-so-pretty picture in code tags to make it display) -



Think components within components (or specifically JPanels within JPanels) until you get what you want.
To have one control top left of the screen, one control top middle and one control top right of the screen, I would -

Set the Layout Manager for the JFrame's content pane to BorderLayout
Create a JPanel to sit along the top (North) border of the screen - and set the Layout of this to BorderLayout

Create three more panels and add these to the top panel in EAST, CENTER and LEFT position.
Add the top panel to the JFrame in North position.
Your 3 sub-panels will now be top left, top middle and top right of the screen. You can add controls to them as you wish.



If you need to do anything more complicated, you might want to start looking at GridBagLayout or SpringLayout - and you can always combine these with other layouts.

[ May 19, 2004: Message edited by: Greg Till ]
[ May 19, 2004: Message edited by: Greg Till ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing - valign