wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Layout Managers & Proper Spacing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Layout Managers & Proper Spacing" Watch "Layout Managers & Proper Spacing" New topic
Author

Layout Managers & Proper Spacing

Brian Drelling
Ranch Hand

Joined: Oct 02, 2009
Posts: 42
So, I have a simple GridBagLayout setup with three columns. In the top center column, I have a label with the title. In the second, third, and fourth rows, I have a label in the first column and a textarea/textfield in the second two columns. Finally, on the bottom, I have a button in each column.

My problem is that the spacing between the label and the textarea/textfields is great, but the spacing between the first column and the left side of the entire frame, as well as teh last column and the right side of the frame is just... too thin?

I'm trying to think of the best way to phrase the question...

How can I add spacing ONLY between the left edge of the frame and the left side of the first column, as well as the right edge of the frame and the right side of the last column? Is there any method for doing this?

I can provide pictures if necessary.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Is that GridBagLayout applied to a JPanel which is inside the JFrame, and you want that space outside the JPanel? In that case a custom border for the panel might be the way to go. Or perhaps modify the Insets for the panel?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2156
    
    7
I can provide pictures if necessary.


No, we never want pictures. A picture doesn't show use what your code is like. Instead we rely on a SSCCE, which is used to simplify the problem because we also don't want to look at hundreds of lines of code to try and understand what you are doing.

To expand on Paul's suggestion, you should be able to use an EmptyBorder. If you don't know about Borders then read the section from the Swing tutorial on How to Use Borders.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.

Brian Drelling
Ranch Hand

Joined: Oct 02, 2009
Posts: 42
Paul Clapham wrote:Is that GridBagLayout applied to a JPanel which is inside the JFrame, and you want that space outside the JPanel? In that case a custom border for the panel might be the way to go. Or perhaps modify the Insets for the panel?


GridBagLayout is applied to a JPanel which is inside the JFrame, yes. I want the space that is not outside the JPanel (this would be between the JPanel and the JFrame, which I have using a border like you mentioned), but the space that is just inside the JPanel on the outside. Does that make sense?

If you know HTML/CSS, the best way to phrase it is by using the box model as an example. I am using a mock "margin" by using a border on the JPanel, but how do I create a "padding" for the Panel itself?

Also Rob, the SSCCE link is great, thank you. I didn't know the protocol on all of that. When I meant a picture, though, it is simply meant to show the space I am looking to expand, but I understand when code is necessary.

Also, I've looked at the border examples and they help, and I have used the borders as mentioned above.

Thanks for the help so far guys!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

What's the difference, in practice, between "padding" and just inserting another border between the JPanel and its border? There is such a thing as a CompoundBorder which you could use to do that.
Brian Drelling
Ranch Hand

Joined: Oct 02, 2009
Posts: 42
Shortly after i posted, I discovered Compound bordered. They worked great!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Layout Managers & Proper Spacing
 
Similar Threads
GridBagLayout
Layout Manager for Square game
Textbox width
JTable Sum Column
Swing