Tuesday, November 5, 2013

GridBagLayout

GridBagLayout is a layout manager in AWT package. It is more complex than GridLayout. But it is more powerful or flexible than GridLayout. By using the GridBagLayout, you can have different components of different sizes in a single row or column. When you arrange components on a container, you do not need to specify the number of rows or columns. Instead, you will need to specify the constraints or rules by using the GridBagContraints class. These constraints help GridBagLayout to place the components in the proper places on the container. Below are common constraints used in setting up the grid.

fill allows the components to resize automatically when the row or column expanded or thrinked.
gridwidth specifies the number of columns in the area to display the component.
gridheight specifies the number of rows in the area to display the component.
gridx specifies the column in the grid to display the component. Its value starts from 0 (first column).
gridy specifies the row in the grid to display the component. Its value starts from 0 (first row).


Example: The following code below arranges the components on the JFrame window as shown in the picture.

GridBagLayout


import java.awt.Container;
import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class GridBagLayoutManager {
public static void main(String[] args){
JFrame jf=new JFrame("GridBagLayout");
jf.setSize(500,300);
Container container=jf.getContentPane();
GridBagLayout gb=new GridBagLayout();
container.setLayout(gb);
GridBagConstraints gc = new GridBagConstraints();
JButton bt1=new JButton("Button 1");
JButton bt2=new JButton("Button 2");
//fill components in cells
gc.fill = GridBagConstraints.BOTH;
        gb.setConstraints(bt1, gc);
        gc.gridwidth = GridBagConstraints.REMAINDER;
        gb.setConstraints(bt2, gc);
      container.add(bt1);
container.add(bt2);

//add JTextArea, Button 3, and Button 4 to the second row

JTextArea ja=new JTextArea(10,10);
ja.setLineWrap(true);
ja.setText("GridBagLayout is a layout manager that is more powerful than the GridLayout manager.");
JButton bt3=new JButton("Button 3");
JButton bt4=new JButton("Button 4");
//expand row and column
gc.gridwidth =2; //2 columns
gc.gridheight =3;//3 rows      
gb.setConstraints(ja, gc);
gc.gridwidth = GridBagConstraints.REMAINDER;
//reset row and column
gc.gridwidth =1;
gc.gridheight = 1;
gb.setConstraints(bt3, gc);
gc.gridy=2; //Add the Button 4 below the Button 3
gb.setConstraints(bt4, gc);
container.add(ja);
container.add(bt3);
container.add(bt4);


jf.setVisible(true);

}


}


30 comments:

  1. If you have example with table, I prefer.

    ReplyDelete
  2. The blog or and best that is extremely useful to keep I can share the ideas of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.

    Digital Marketing Course in Chennai
    Digital Marketing Training in Chennai

    ReplyDelete
  3. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
    ccna Training center in Chennai
    Best CCNA Training Institute in Chennai
    ccna certification in Chennai
    ccna course in Chennai
    German Language Course in Chennai
    German Courses in Chennai

    ReplyDelete
  4. I read your blog regularly from the beginning. It was very impressed to me and I learn huge of information from your blog. Thank you!!!
    Ethical Hacking Course in Chennai
    Hacking Course in Chennai
    Certified Ethical Hacking Course in Chennai
    Ethical Hacking Training in Chennai

    ReplyDelete
  5. You gave your ideas in an effective way which will reach the readers more and it will impact the others wrong thoughts.thanks for giving the commenting opportunity.
    Software testing training in chennai
    Salesforce Training in Chennai
    German Classes in Chennai
    mobile application development training in chennai
    ios training in OMR
    ios training in porur

    ReplyDelete
  6. Greetings from Carolina! I'm bored at work so I decided to check out your blog on my iphone during lunch break. I really like the knowledge you provide here and can't wait to take a look when I get home. I'm surprised at how quick your blog loaded on my phone .. I'm not even using WIFI, just 3G .. Anyways, very good blog! clear bra ny

    ReplyDelete
  7. Thank you, I've just been searching for information approximately this topic for a while and yours is the best I have discovered till now. But, what concerning the conclusion? Are you positive concerning the source?best thing for cellulite on thighs

    ReplyDelete
  8. My programmer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the expenses. But he's tryiong none the less. I've been using WordPress on several websites for about a year and am nervous about switching to another platform. I have heard fantastic things about blogengine.net. Is there a way I can transfer all my wordpress posts into it? Any help would be really appreciated!womens toe post sandals

    ReplyDelete
  9. Please let me know if you're looking for a author for your blog. You have some really good articles and I think I would be a good asset. If you ever want to take some of the load off, I'd love to write some articles for your blog in exchange for a link back to mine. Please shoot me an email if interested. Kudos! engage with followers

    ReplyDelete
  10. I was in search of this blog for a while and just now got this into my vision. Thanks for sharing

    ReplyDelete
  11. Excellent Blog!!! Such an interesting blog with clear vision, this will definitely help for beginner to make them update.

    ReplyDelete
  12. Ethical Hacking Course in Chennai

    I am glad that I have visited this blog. Really helpful, eagerly waiting for more updates.

    ReplyDelete
  13. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information..put somemore info.i may help us.
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  14. Python Training in Chennaii
    The information given in this blog is very nice and i like it if you share more this kind of blog and i also have written this kind of blog you can also read for more knowledge.

    ReplyDelete
  15. angularjs interview questions and answers for experienced
    Important AngularJS Interview Questions and Answers for freshers and experienced to get your dream job in Angular! Basic & Advanced AngularJS Interview Questions for Freshers & Experienced.

    ReplyDelete
  16. javascript interview questions and answers pdf
    Important Javascript Interview Questions and Answers for freshers and experienced to get your dream job & Advanced Javascript Interview Questions for Freshers & Experienced.

    ReplyDelete
  17. Thanks a lot for writing this detailed article on how can I use GridBagLayout in Java when I am shifting components from rows to columns. Now I can shift my components easily and make this process less complex. Coursework Writing Services

    ReplyDelete