Forums Register Login

grid bag constraints problem

+Pie Number of slices to send: Send
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










+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
 

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?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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)
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 ??



+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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
There's a city wid manhunt for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 839 times.
Similar Threads
no suitable constructor found
JDialog Box closes main application
components dont show after i add them using GridBagLayout...What am i doing wrong
Button and Keyboard linking
grid back gridx
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:39:43.