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);

}


}


40 comments:

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

    ReplyDelete
  2. The article is too good and it will be helpful for java developers.Thanks for sharing such useful information.
    Java training in Chennai


    ReplyDelete
  3. 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
  4. 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
  5. 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
  6. This information is impressive; I am inspired with your post. Keep posting like this, This is very useful.Thank you so much. Waiting for more blogs like this.
    Aviation Academy in Chennai
    Aviation Courses in Chennai
    aviation institute in chennai
    best aviation academy in chennai

    ReplyDelete
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. DevOps Training in Chennai
    I am glad that I have visited this blog. Really helpful, eagerly waiting for more updates.

    ReplyDelete
  13. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    JavaScript Training in Chennai

    JavaScript Training in Chennai

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

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

    ReplyDelete
  16. C C++ Training in Chennai
    Excellent blog with lots of information. I have to thank for this. Do share more.

    ReplyDelete
  17. Ethical Hacking Course in Chennai

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

    ReplyDelete
  18. It's very useful blog post with inforamtive and insightful content and i had good experience with this information.I have gone through CRS Info Solutions Home which really nice. Learn more details About Us of CRS info solutions. Here you can see the Courses CRS Info Solutions full list. Find Student Registration page and register now.Find this real time DevOps Training and great teaching. Join now on Selenium Training online course. Upskill career with Tableau training by crs info solutions. Latest trending course is Salesforce Lightning training with excellent jobs.

    ReplyDelete
  19. 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
  20. 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
  21. gmat coaching institutes in chennai
    Having a good score in GMAT is very important to get into your dream university. Choose the best GMAT coaching centres in Chennai from the list.

    ReplyDelete
  22. 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
  23. 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