BorderLayout without a center area

Hi, I'm trying to use BorderLayout, and I like how it sets up the window except for how all the examples I've seen all have a center part to it. It's that center part that I'd like to get rid of, but with certain conditions. For instance, let's say I have the following code:

(From tutorial on borderlayout)

...//Container pane = aFrame.getContentPane()...

JButton button =new JButton("Button 1 (PAGE_START)");

pane.add(button, BorderLayout.PAGE_START);

//Make the center component big, since that's the

//typical usage of BorderLayout.

//button = new JButton("Button 2 (CENTER)");

//button.setPreferredSize(new Dimension(200, 100));

//pane.add(button, BorderLayout.CENTER);

button =new JButton("Button 3 (LINE_START)");

pane.add(button, BorderLayout.LINE_START);

button =new JButton("Long-Named Button 4 (PAGE_END)");

pane.add(button, BorderLayout.PAGE_END);

button =new JButton("5 (LINE_END)");

pane.add(button, BorderLayout.LINE_END);

The program I'm making can only contain 4 containers to it, not 5 (In this code the buttons act as JPanels or containers). I guess I could squish the center portion to be only like 1 pixel in width, almost diminishing it, but I'd like to get rid of it totally, having the "LINE_START" and "LINE_END" parts touching. I tried simply getting rid of the center part (I commented it out in the code above) but that just left a "hole" in the middle.

I also substituted these parameters with NORTH, SOUTH, etc but to no effect. Any suggestions would be great. Thanks.

[2037 byte] By [Jason102a] at [2007-10-3 3:49:58]
# 1
Try using other Layout, like GridLayout. which would satisfy your design, else, try setting it to null, and you could position your components manually(x & y coordinates).-Nywled
Redxxiva at 2007-7-14 21:47:18 > top of Java-index,Desktop,Core GUI APIs...
# 2

seems to be OK, if pack() is used

import java.awt.*;

import javax.swing.*;

class Testing extends JFrame

{

public Testing()

{

setLocation(300,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

Container pane = getContentPane();

JButton button = new JButton("Button 1 (PAGE_START)");

pane.add(button, BorderLayout.PAGE_START);

button = new JButton("Button 3 (LINE_START)");

pane.add(button, BorderLayout.LINE_START);

button = new JButton("Long-Named Button 4 (PAGE_END)");

pane.add(button, BorderLayout.PAGE_END);

button = new JButton("5 (LINE_END)");

pane.add(button, BorderLayout.LINE_END);

pack();

}

public static void main(String[] args){new Testing().setVisible(true);}

}

Michael_Dunna at 2007-7-14 21:47:18 > top of Java-index,Desktop,Core GUI APIs...
# 3

Ok I'm following you on that, but then other complications come up. I guess I'd now have a main GridLayout that is (3,1) in size and a sub GridLayout in the center cell of the main one that is (1,2). That's great with the exception that all the cells in a GridLayout layout are equal in size by default. In other words I'd like to make it where the height of the top cell in the main GridLayout is fixed and only the width can change to the resizing of the window. I'd also like to have those properties applied to the bottom cell in the main one too. Only the middle cell containing the sub GridLayout that is (1,2) in size should be able to stretch freely in terms of height and width when resizing the window. Is this setup possible, and, if it is, how can I fix the height of the top and bottom cell to a certain value? Again, thanks for the help.

Jason102a at 2007-7-14 21:47:18 > top of Java-index,Desktop,Core GUI APIs...
# 4
Whoops sorry Michael didn't notice your post let me check what you've suggested first.
Jason102a at 2007-7-14 21:47:18 > top of Java-index,Desktop,Core GUI APIs...
# 5
Nope sorry Michael I guess you didn't understand my question because your code still contains a "hole" where the center portion should be in the BorderLayout when I resize the window. Any more help will be greatly appreciated! My next question is right after Michael's response. Thanks!
Jason102a at 2007-7-14 21:47:18 > top of Java-index,Desktop,Core GUI APIs...
# 6

try this variation, but center will stretch if frame is made taller.

import java.awt.*;

import javax.swing.*;

class Testing extends JFrame

{

public Testing()

{

setLocation(300,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

Container pane = getContentPane();

JButton button = new JButton("Button 1 (PAGE_START)");

pane.add(button, BorderLayout.PAGE_START);

button = new JButton("Button 3 (LINE_START)");

//pane.add(button, BorderLayout.LINE_START);

JPanel p = new JPanel(new GridLayout(1,2));

p.add(button);

button = new JButton("Long-Named Button 4 (PAGE_END)");

pane.add(button, BorderLayout.PAGE_END);

button = new JButton("5 (LINE_END)");

//pane.add(button, BorderLayout.LINE_END);

p.add(button);

pane.add(p,BorderLayout.CENTER);

pack();

}

public static void main(String[] args){new Testing().setVisible(true);}

}

Michael_Dunna at 2007-7-14 21:47:18 > top of Java-index,Desktop,Core GUI APIs...
# 7

Hey that helps! We're getting somewhere now. I'm guessing the top and bottom portions height's of the BorderLayout will be set according to what and how many components are in them? Anyway here's my next question: How can I set the width for the left cell in the GridLayout? I don't mind it increasing in height when the window is resized nor do I mind the right cell freelly stretching both ways. It's just that I've never seen it where one of the cells in a GridLayout is set to a certain size, like it's width, because by default the 2 cells stay the same at all times. How can I do this?

Jason102a at 2007-7-14 21:47:19 > top of Java-index,Desktop,Core GUI APIs...
# 8

[url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How Use Use Layout Managers[/url].

You need to learn the characteristics of each LayoutManager and then mix and match Layout Managers to get your desired effect.

> How can I set the width for the left cell in the GridLayout?

You can't. By definition all cells in a GridLayout are the same. But maybe you can use a BorderLayout. Add a component to the West and the second component to the center. As you know extra space is added or removed from the Center as a frame is resized.

camickra at 2007-7-14 21:47:19 > top of Java-index,Desktop,Core GUI APIs...
# 9

you will have to experiment with nested layouts

gridlayouts are all the same size, however you can put component/s in panels

then put the panel in the gridlayout cell - the panel will take up additional space.

from the example in my last post, if you want west and east to be as tall as the

frame, instead of between north/south, put north and south into a borderlayout

panel, then this panel is added to center of the contentPane.

so, basically, mix'n'match, see what happens.

Michael_Dunna at 2007-7-14 21:47:19 > top of Java-index,Desktop,Core GUI APIs...
# 10
Gotcha. I'll play around with it for a bit and if I have any more questions on this topic I'll go ahead and add it on to this thread. Thanks a ton guys.
Jason102a at 2007-7-14 21:47:19 > top of Java-index,Desktop,Core GUI APIs...
# 11
Whatever you do, please never, ever heed the suggestion to "try setting it to null, and you could position your components manually(x & y coordinates)"
itchyscratchya at 2007-7-14 21:47:19 > top of Java-index,Desktop,Core GUI APIs...
# 12
Oh why would that be such a bad thing to do?
Jason102a at 2007-7-14 21:47:19 > top of Java-index,Desktop,Core GUI APIs...
# 13

> Oh why would that be such a bad thing to do?

Click on the link I gave you above. Then click or the "Previous" link to go back one level in the tutorial. Then click on the "Absolute Positioning" section of the tutorial. Read the first paragraph, which tells you why its not a good idea and the second paragraph which tells you when you might want to use it.

camickra at 2007-7-14 21:47:19 > top of Java-index,Desktop,Core GUI APIs...
# 14

Thanks camickr that was good info. That reminded me on one thing, however. I always like to use the (frameName).setLocationRelativeTo(null); command to conveniently center the windows when I run the program. I know that what we're talking about are layouts and components within the window, but do you think using this command for displaying the window is still a good idea?

Jason102a at 2007-7-14 21:47:19 > top of Java-index,Desktop,Core GUI APIs...
# 15
> but do you think using this command for displaying the window is still a good idea? Yes.
camickra at 2007-7-21 10:18:54 > top of Java-index,Desktop,Core GUI APIs...
# 16
Ok and again thanks guys.
Jason102a at 2007-7-21 10:18:54 > top of Java-index,Desktop,Core GUI APIs...