Uniform size in components added using GridBagLayoutManager

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • heat84
    New Member
    • Nov 2007
    • 118

    Uniform size in components added using GridBagLayoutManager

    Hie , I am adding three buttons to my frame but when I am running the code , the buttons do not have uniform width. Can someone help me out.- I need ideas on how to make btnA,btnB and btnC have the same width. The code is not giving errors when I run it. The code is

    NB: My class extends JFrame

    Code:
    Container cont=getContentPane();
        	JPanel panel=new JPanel(new GridBagLayout());
        	GridBagConstraints gbc= new GridBagConstraints();
        	gbc.weighty=0.5;
        	//gbc.fill=GridBagConstraints.HORIZONTAL;
        	gbc.weightx=0.5;
        	gbc.gridx=0;
        	gbc.gridy=0;
        	btnA=new JButton("AAAAA   AAAAA");
        	panel.add(btnA,gbc);
        	gbc.gridy=1;
        	btnB = new JButton("BBBB BBBBBBBBB");
        	gbc.gridy=2;
        	panel.add(B,gbc);
        	btnC=new JButton("CCCCCCCCC");
        	gbc.gridy=3; 
            panel.add(btnC,gbc);   	
            setSize(300,200);      	
          	cont.add(panel);
          	setVisible(true);
          	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    If you really want them to be the same size use a GridLayout instead;
    specify one single row and your buttons end up next to each other, all of the
    same size.

    kind regards,

    Jos

    Comment

    • heat84
      New Member
      • Nov 2007
      • 118

      #3
      Originally posted by JosAH
      If you really want them to be the same size use a GridLayout instead;
      specify one single row and your buttons end up next to each other, all of the
      same size.

      kind regards,

      Jos
      Thanks-
      The buttons are arranged vertically so I want them to have the same width but not height. However , if there is no way of manouvering except changing the layout manager to GridBagLayout , then I will settle for that.

      Comment

      • heat84
        New Member
        • Nov 2007
        • 118

        #4
        Originally posted by JosAH
        If you really want them to be the same size use a GridLayout instead;
        specify one single row and your buttons end up next to each other, all of the
        same size.

        kind regards,

        Jos
        Thanks-
        The buttons are arranged vertically so I want them to have the same width,the height is not a problem to me. However , if there is no way of manouvering except changing the layout manager to GridLayout , then I will settle for that.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by heat84
          Thanks-
          The buttons are arranged vertically so I want them to have the same width,the height is not a problem to me. However , if there is no way of manouvering except changing the layout manager to GridLayout , then I will settle for that.
          A GridLayout can also arange components vertically: for your three buttons
          specify three rows in the GridLayout.

          kind regards,

          Jos

          Comment

          • heat84
            New Member
            • Nov 2007
            • 118

            #6
            Originally posted by JosAH
            A GridLayout can also arange components vertically: for your three buttons
            specify three rows in the GridLayout.

            kind regards,

            Jos
            Thank you Jos , you can now proceed with fighting the politicians .

            Comment

            Working...