• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Form layout using GridBagLayout

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your SSCCE (<= click link to find out what SSCCE really means)
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posting the complete code

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still facing the problem.
 
Nam Ha Minh
Ranch Hand
Posts: 504
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use insets, instead of internal padding (ipadx, ipady). For example:

gbc.insets = new Insets(10, 10, 10, 10);
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think SpringLayout will work much better.
 
Nam Ha Minh
Ranch Hand
Posts: 504
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Punit Jain
Ranch Hand
Posts: 1014
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies.

I want the form to appear on the north west side. Should i go for SpringLayout?
 
Hitesh P Patel
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Darryl Burke, Santhosh San, & others for your reply.

I am using Spring Layout & it works very well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic