aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Form layout using GridBagLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Form layout using GridBagLayout" Watch "Form layout using GridBagLayout" New topic
Author

Form layout using GridBagLayout

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I am creating a student form, presently with just name and city field.

What i want is that there should be perfect gap between label and textfield. Here is what i created



But, the textfield appears just after its associated Label.

Moreover, the form appears in the middle of the applet

Is something wrong with the code?


kunal
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Post your SSCCE (<= click link to find out what SSCCE really means)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Posting the complete code

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Inserting after line number 32 produces space between the label and textfield, but that's too much. now the label is on the one end (extreme left) and textfield is on the extreme right
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Still facing the problem.
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

You should use insets, instead of internal padding (ipadx, ipady). For example:

gbc.insets = new Insets(10, 10, 10, 10);


PM Me If You Want to get Urgent Help on Java Programming
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks for your reply Nam Ha Minh. That works.

Moreover, the form appears in the middle of the applet


But, still the components are displayed in the middle of panel. Is that some default behavior?
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I think SpringLayout will work much better.
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

Kunal Lakhani wrote:Thanks for your reply Nam Ha Minh. That works.

Moreover, the form appears in the middle of the applet


But, still the components are displayed in the middle of panel. Is that some default behavior?


Well, you didn't specify how do you want the form appears.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Kunal Lakhani wrote:the components are displayed in the middle of panel. Is that some default behavior?

Read the API for GridBagConstraints#weightx/weighty. If both are zero for all rows/columns, extra space isn't allocated.


luck, db
There are no new questions, but there may be new answers.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
Kunal Lakhani wrote:Thanks for your reply Nam Ha Minh. That works.

Moreover, the form appears in the middle of the applet


But, still the components are displayed in the middle of panel. Is that some default behavior?


Add your panel, with BorderLayout.NORTH.
i mean like this,


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

That only works if the container uses a BorderLayout. If it uses a GridBagLayout that will do nothing at best, and throw an exception at worst.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks for your replies.

I want the form to appear on the north west side. Should i go for SpringLayout?
Hitesh P Patel
Greenhorn

Joined: May 23, 2012
Posts: 8
Add gbc.weighty = 1; to the cityField, constraints.anchor = GridBagConstraints.NORTHWEST and add panel to BorderLayout.WEST
Addition/Changes made to your code are commented
Santhosh San
Greenhorn

Joined: Sep 16, 2009
Posts: 2
Use netbeans , you can create GUI easily and later on you can change your Layout for same form.
this way is little more time consuming , at times.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Santhosh San wrote:Use netbeans , you can create GUI easily and later on you can change your Layout for same form.
this way is little more time consuming , at times.

Bad advice. Using a visual designer / code generator without adequate understanding of the gobbledygook it generates will never be faster than hand coding.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Darryl Burke, Santhosh San, & others for your reply.

I am using Spring Layout & it works very well.
 
Consider Paul's rocket mass heater.
 
subject: Form layout using GridBagLayout