• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

grid bag constraints problem

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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










 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic