Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using GridBagLayout in Swing  RSS feed

 
Derrick Stevens
Greenhorn
Posts: 1
Chrome Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is a post that could better help me, please direct me. I searched but what I found didn't necessarily help. I was taught Java, but not necessarily any GUI aspects, other then my teacher saying "Here's a packet, read it," which, you know, only got me so far.

So... I've been trying to use a LayoutManager to layout the buttons I want for the main screen of my program and I decided that GridBagLayout sounded the best. Yes, I know, everywhere I read says it's horribly frustrating, inefficient, but flexible. But from looking at the other managers, this seemed to be what I wanted.

I have a picture of what I'm TRYING to do with these buttons.


Now here's the code I have. I can post the main method, but it's only two lines :P



I included the components before the constructor just in case those were an issue as well.

Now the problem I'm having right now, besides not really knowing how to use Swing, is that if I run this, nothing shows up on the screen. But if I remove mainPane from setLayout, buttons show up in a horizontal row at top. It may not even be a GridBagLayout problem, it may be that I don't know how to use JPanel.

I've tried reading online, but I just can't seem to find where I went wrong, besides the obvious that I know I did. I'll appreciate any help that comes my way.

~Kune
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of comments:

1) When you post code it should be executable, so yes the main() method should be included with the class so we don't have to make up our own.

2) There are two problems using "Window" as a class name. First it is not descriptive, second a Window class already exists in the API so you should never use an existing API name.

3) When using layout managers you are never forced to only use a single layout manager. You can created nested panels that each use a different layout. For example you might create a top panel for the vertical buttons and use a vertical BoxLayout. Then for the bottom panel you can use a horizontal BoxLayout. Even easier you can use the Box class in both cases.

4) When writing code you would generally add the components to the panel in a top/left to bottom right order. This makes it easier to get the contraints consistent.

5) By default a JPanel uses a FlowLayout. That is why you see the components in a horizontal row when you don't specify a specific layout manager

6) It looks to me like your usage of the weightX/Y parameters is wrong. Read the section from the Swing tutorial on How to Use Grid Bag Layout for explanations of all the parameter and a working example.

7) General advice is to simplify the code. You say not components are displaying. Well write the code with only one button first. Then when that displays you add the second and so on. The less lines of code you have to look at the easier it is to understand or see where you might have made a mistake.

8) Many people when using a GBL will only use a single constraint and then change only the constraints that need to be changed. I never use GBL so I don't know much about it. But I would guess you you want all components to appear at their preferred size so the only constraints you need to change may be the gridX/Y for each button. GLB doesn't automatically add space between each component so you may need to use insets as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!