GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes BorderLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "BorderLayout" Watch "BorderLayout" New topic
Author

BorderLayout

Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Hi,

I am trying to do this.





So far I have got the buttons but they are on the top and I need them to be on the left hand side. I have tried changing the borderlayout from NORTH to EAST but it stays the same.





Can anyone tell me where I am going wrong?

The code I have so far is.









Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
You appear to be adding Components without giving a location (eg EAST, WEST). Have a look at the Java™ Tutorials section. I suspect you want a different Layout, maybe Grid.

Moving to our GUIs forum, where we usually discuss this sort of thing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
You are also adding things before you set the Layout. That is probably a mistake. Set the Layout first.
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Thanks for the reply.

So would I have something like.


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Not quite. That would be using a GridLayout, but then adding the component with a BorderLayout constant, which is irrelevant to a GridLayout.

You need to give your main panel a BorderLayout, and your sub-panel a GridLayout. Then add the sub-panel to the main panel (with position BorderLayout.WEST), and the buttons to the sub-panel (they don't need a constant).

Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Thanks.

I'm not sure I understand how to do that 100% but am I anywhere near with this.


pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ross McManus wrote:Thanks.

I'm not sure I understand how to do that 100% but am I anywhere near with this.




You are adding the Buttons JPanel to two components and this is won't work. You may benefit greatly by reading the tutorial more and doing less guessing. It's all in there.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Going through my earlier post in more detail:

"You need to give your main panel a BorderLayout, ..."
"...and your sub-panel a GridLayout."
"Then add the sub-panel to the main panel (with position BorderLayout.WEST), ..."
"...and the buttons to the sub-panel (they don't need a constant).

Going through the Sun tutorial in detail is a good idea, though. Otherwise you're going to have this problem every time you have a different layout.
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Thanks for the detailed reply, I have the buttons done now.

I also have done the text in the grid that I have to do but have one question about it.

I have managed to get the text in a grid layout for that but I'm not sure how to get the black lines that divide them up on if you know what I mean like it shows in the picture in my first post.

This is what mine looks like.




This is the code I have for that part.



Also when I run it the box is small unless I drag and expand it to make it look the normal size, Is there any way to set it so it is the right size when I run it?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Check out the documentation for the setBorder method (and the BorderFactory class). The method belongs to the JComponent class, so borders can be applied to any Swing components - including labels.

For the size - you can use setSize on your top-level component (probably a JFrame?) before you make it visible.
Robert Staniforth
Greenhorn

Joined: Aug 03, 2010
Posts: 11
hey, i'm doing the same thing as Ross and i've got stuck too..
It looks like they have two grids for the different types of rooms, as you see the Meeting Rooms are bigger than the Double rooms and they have a separate border. is it possible to have two grids in one area? I was trying to put two grids in BorderLayout.CENTER but it failed completely
how do you do this? i'm completely baffled

-edit
in my search to find the answer I read about JTables, is it possible they have this in a table?
i don't know Java that well but my theory was put a 2 row (1 column) table with a grid in the top for the single/double rooms and another grid with the meeting rooms in the bottom.. is that possible?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BorderLayout