Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

grid bag constraints problem  RSS feed

 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I'm trying to use gridbagconstraints in a small app that i'm making but for some reason it won't align to the way I want it, here is my source code from my 4 classes










 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Mitchell,

well, some remarks could be made, but for now:

have a look at your FormPanel class, line 24. What do you noticë?
Next, have a careful look at what you specify as GridBagConstraints.gridx and gridy.

Let us know what you think (or, having done some repairs, how it looks).
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have a look at your FormPanel class, line 24. What do you noticë?


There is nothing on line 24?

Next, have a careful look at what you specify as GridBagConstraints.gridx and gridy.

I'm looking at it now and starting to think that it's not working because of the following lines



Is it because i've specified the nameLabel and nameField with the same position of
con.gridx = 0 and con.gridy = 0?

 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, line 24 is:

So, you do create a JPanel with a GridBagLayout. But is that the JPanel to which you are adding your components?

And indeed: your gridx and gridy need to be different for each component.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you do create a JPanel with a GridBagLayout. But is that the JPanel to which you are adding your components?


Yes, because there is no other JPanel created in the application, unless i'm missing something or it doesn't make sense to you?


And indeed: your gridx and gridy need to be different for each component.


That was a silly mistake but as soon as I thought hard about it I revisited my notes and realized my mistake, but thanks for that
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell bat wrote:
Yes, because there is no other JPanel created in the application, unless i'm missing something or it doesn't make sense to you?

Are you sure there is only one panel involved? (hint: have a look at your FormPanel class definition)
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure there is only one panel involved? (hint: have a look at your FormPanel class definition)


So you're telling me that doesn't belong there?
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed.

Your FormPanel IS a JPanel. That is why in the constructor your add(component...) works. It simply adds these components to this instance of FormPanel.
But the default LayoutManager is a FlowLayout, and since you did not replace that Layout (with : setLayout(...)), you are still using this FlowLayout.

That famous line 24 did create a JPanel with a GridBagLayout, but that was all... so indeed, drop that line, because it is doing nothing.

So, how do we get this instance of FormPanel to use a GridBagLayout? Well, if you use an IDE, tyoe, in the contructor: this.

You will now see a pop-up dialog, in which you see all the methods that are available. One of them is : setLayout.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you work out how many panels you have got, I suggest you give up plain simple constraints and find out about Cay Horstmann's GBC class which makes it much easier to use grid bag.
[edit:]Do your text fields appear correctly? I think they will shrink almost to invisibility if you don't give them a HORIZONTAL fill.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your FormPanel IS a JPanel. That is why in the constructor your add(component...) works. It simply adds these components to this instance of FormPanel.


So by having formPanel extending JPanel I automatically inherit all of the properties of the JPanel class therefore not needing to create a new JPanel object?

But the default LayoutManager is a FlowLayout, and since you did not replace that Layout (with : setLayout(...)), you are still using this FlowLayout.


So for this in the ToolBar class line 15 change it from to ??



 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do your text fields appear correctly? I think they will shrink almost to invisibility if you don't give them a HORIZONTAL fill.


They actually look the same which is good but why would I give them a horizontal fill for? It looks fine when I run it
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell bat wrote:So for this in the ToolBar class line 15 change it from to ?

Well, you could if you feel like it, but personally, I liked the three buttons, neatly side by side in the toolbar! So the FlowLayout seems good enough.

By the way: as far as I know, there is no GridBagLayout constructor that has a parameter.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could if you feel like it, but personally, I liked the three buttons, neatly side by side in the toolbar! So the FlowLayout seems good enough.

By the way: as far as I know, there is no GridBagLayout constructor that has a parameter.


Cheers for the help mate!

Just wanted to say that you guys here at Code Ranch are amazing
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!