IGNOU MBA Assignment, Solved IGNOU MBA Assignments

What is Grid Baglayout in Java?

Layout managers are software components utilized in widget toolkits that have the cabability to lay out widgets by their relative positions without needing distance units. It is usually natural to define component layouts in this way rather than specify their location in pixels or typical distance units, so numerous popular widget toolkits consist of this capability by default.

GridBagLayout is probably the most flexible – and sophisticated – layout managers of the Java platform. A GridBagLayout places components in a grid of rows and columns, enabling specific components to cover several rows or columns. Don’t assume all rows essentially have the identical height. Likewise, only some columns have the identical width. Basically, GridBagLayout places components in rectangles (cells) in a grid, and then uses the components’ ideal sizes to figure out how large the cells needs to be.

The following code creates the GridBagLayout and the components it manages.

JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}button = new JButton(“Button 1″);
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);button = new JButton(“Button 2″);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);button = new JButton(“Button 3″);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);button = new JButton(“Long-Named Button 4″);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);

button = new JButton(“5″);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);

You can find the entire source file in GridBagLayoutDemo.java

GridBag Layout Example – A Simple Form Layout