Back to the home page of this site This applets illustrates effect of changing the pad values of components positioned by the GridBagLayout manager. Moving the scrollbar changing the values of ipadx and ipady for button six. Each button is positioned by setting its gridx and gridy values. The GridBagLayout manager deduces how many cells are in the grid, unlike the GridLayout for which you set up the number of rows and columns by hand.



//Source code to the applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class GridBagCons extends Applet {

Button btnOne    = new Button("One");
Button btnTwo    = new Button("Two");
Button btnThree  = new Button("Three");
Button btnFour   = new Button("Four");
Button btnFive   = new Button("Five");
Button btnSix    = new Button("Six");

Label lblNorth = new Label();

GridBagLayout gbl = new GridBagLayout();
GridLayout glNorth = new GridLayout(2,1);
BorderLayout layBord = new BorderLayout();

GridBagConstraints gbc= new GridBagConstraints();

Panel p = new Panel();
Panel pNorth = new Panel();

//Scrollbar scrParm = new Scrollbar(Scrollbar.HORIZONTAL,360,5, 0,724);
Scrollbar scrParm = new Scrollbar(Scrollbar.HORIZONTAL,1,1, 0,100);

  public GridBagCons() {

  }


  public void init() {
    jbInit();
  }


  private void jbInit(){
    setLayout(layBord);
    scrParm.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        scrParm_adjustmentValueChanged(e);
      }
    });

    //add(scrParm,"North");

    pNorth.setLayout(glNorth);
    pNorth.add(scrParm);
    pNorth.add(lblNorth);

    pNorth.setBackground(Color.pink);
    add(pNorth,"North");

    p.setLayout(gbl);


    gbc.gridx=0;
    gbc.gridy=0;
    gbl.setConstraints(btnOne,gbc);
    p.add(btnOne);

    gbc.gridx=1;
    gbc.gridy=0;
    gbl.setConstraints(btnTwo,gbc);
    p.add(btnTwo);

    gbc.gridx=2;
    gbc.gridy=0;
    gbl.setConstraints(btnThree,gbc);
    p.add(btnThree);

    //Second row

    gbc.gridx=0;
    gbc.gridy=1;
    gbl.setConstraints(btnFour,gbc);
    p.add(btnFour);

    gbc.gridx=1;
    gbc.gridy=1;
    gbl.setConstraints(btnFive,gbc);
    p.add(btnFive);

    gbc.gridx=2;
    gbc.gridy=1;
    gbl.setConstraints(btnSix,gbc);

    p.add(btnSix);

    add(p);
          }

  void scrParm_adjustmentValueChanged(AdjustmentEvent e) {

   int iParmVal = e.getValue();

   System.out.println("getValue : "+ e.getValue());
   lblNorth.setText("ipadx and ipady values for Button six ="+iParmVal );

   gbc.ipadx=iParmVal;
   gbc.ipady=iParmVal;

   gbl.setConstraints(btnSix,gbc);
   p.setLayout(gbl);
   p.validate();



  }
}