Win a copy of Penetration Testing Basics this week in the Security forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

faceing problem while using GridBagLayout

sun moon
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is taken directly from the jdk1.2 specification.
It works fine.
import java.awt.*;
import java.util.*;
import java.applet.Applet;
public class GridBagEx1 extends Applet {
protected void makebutton(String name,
GridBagLayout gridbag,
GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setFont(new Font("Helvetica", Font.PLAIN, 14));
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c);
c.weightx = 0.0; //reset to the default
makebutton("Button5", gridbag, c); //another row
c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
makebutton("Button6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button7", gridbag, c);
// @@@@@from here
c.gridwidth = 1;
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c);
c.weighty = 0.0;
c.gridwidth = GridBagConstraints.REMAINDER; c.gridheight = 1;
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c);
// @@@@@tell here
setSize(300, 100);
public static void main(String args[]) {
Frame f = new Frame("GridBag Layout Example");
GridBagEx1 ex1 = new GridBagEx1();
f.add("Center", ex1);

If i remove the lines from /@@@@fromhere to //@@@tellhere(which are shown in the code), then the total layout is changing and also if we maximize the window the button 9 and button 10 are see very big(that is occupying entire remaining area).
So my question is i want to print all the buttons in 3 rows and 4 cols in a fixed width and height, that is in same proportion using gridbaglayout is it possible.
Please help me out in this code.

thanks in advance
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic