Results 1 to 10 of 10
  1. #1
    canersen is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Smile GridBagLayout problem!

    I'm a beginner to java GUI programming. I started to learn java GUI with one of the introduction to JAVA GUI ... books. There is example about GridBagLayout layout manager, i tried to write the code, but it is not working as i expected. I provide the code that i test and the figure of desired layout. I appreciate your help.

    Figure of desired layout:


    Obtained wrong layout using the code that i write:


    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Exercise17_3 extends JFrame{
    	private GridBagLayout gbl = new GridBagLayout();
           	public Exercise17_3(){
    
    	Container contentPane = this.getContentPane();
    	contentPane.setLayout(gbl);
    		
    	JPanel panelA = new JPanel();
    	JPanel panelB = new JPanel();
    	JPanel panelC = new JPanel();
    	JPanel panelD = new JPanel();
    	JPanel panelE = new JPanel();
    	JPanel panelF = new JPanel();
    	JPanel panelG = new JPanel();
    	JPanel panelH = new JPanel();
    	JPanel panelJ = new JPanel();
    
    	panelA.setBorder(BorderFactory.createTitledBorder("panelA"));
    	panelB.setBorder(BorderFactory.createTitledBorder("panelB"));
    	panelC.setBorder(BorderFactory.createTitledBorder("panelC"));
    	panelD.setBorder(BorderFactory.createTitledBorder("panelD"));
    	panelE.setBorder(BorderFactory.createTitledBorder("panelE"));
    	panelF.setBorder(BorderFactory.createTitledBorder("panelF"));
    	panelG.setBorder(BorderFactory.createTitledBorder("panelG"));
    	panelH.setBorder(BorderFactory.createTitledBorder("panelH"));
    	panelJ.setBorder(BorderFactory.createTitledBorder("panelJ"));
    
    	this.getContentPane().add(panelA);
    	this.getContentPane().add(panelB);
    	this.getContentPane().add(panelC);
    	this.getContentPane().add(panelD);
    	this.getContentPane().add(panelE);
    	this.getContentPane().add(panelF);
    	this.getContentPane().add(panelG);
    	this.getContentPane().add(panelH);
    	this.getContentPane().add(panelJ);
    
    	easyConstraints(gbl,panelA,1,3,0,0,1.0,1.0);
    	easyConstraints(gbl,panelB,1,2,1,0,1.0,1.0);
    	easyConstraints(gbl,panelC,1,1,2,0,1.0,1.0);
    	easyConstraints(gbl,panelD,1,2,3,0,1.0,1.0);
    	easyConstraints(gbl,panelE,1,2,2,1,1.0,1.0);
    	easyConstraints(gbl,panelF,1,1,1,2,3.0,2.0);
    	easyConstraints(gbl,panelG,1,1,3,2,1.0,1.0);
    	easyConstraints(gbl,panelH,2,1,0,3,1.0,1.0);
    	easyConstraints(gbl,panelJ,2,1,2,3,1.0,1.0);
    
    	}
    
    	private void easyConstraints(GridBagLayout GBL, JComponent Comp,
    	int w, int h, int x, int y, double wx, double wy){
    		GridBagConstraints constraints = new GridBagConstraints();
    		constraints.fill = GridBagConstraints.BOTH;
    		constraints.gridwidth=w;
    		constraints.gridheight=h;
    		constraints.gridx=x;
    		constraints.gridy=y;
    		constraints.weightx=wx;
    		constraints.weighty=wy;
    		GBL.setConstraints(Comp,constraints);
    	}
    
    	public static void main(String[] args){
    		Exercise17_3 gridbagFrame = new Exercise17_3();
    		gridbagFrame.setTitle("Grid-Bag Layout");
    		gridbagFrame.setSize(300,250);
    		gridbagFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		gridbagFrame.setResizable(false);
    		gridbagFrame.setLocation(680,0);
    		gridbagFrame.setVisible(true);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    canersen is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Is there a mistake in the code which results in wrong layout. I need a comment from a view of senior coder...
    If i can not solve it would i will jump to another method, but leaving it unsolved and jumping to another method bothers me :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by canersen View Post
    Is there a mistake in the code which results in wrong layout. I need a comment from a view of senior coder...
    If i can not solve it would i will jump to another method, but leaving it unsolved and jumping to another method bothers me :)
    The numbers (sizes) for the constraints are incorrect, e.g. panel a has a height of 4, you set it to 3. There are more errors but they're easily solved if you look at the grid markers of your example.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    canersen is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    In the book it says weights will scale the cells, i tried to scale F cell (3.0,2.0) since it is comman but it doest work. Can you help me correcting the constraints.

    Java Code:
     
    	easyConstraints(gbl,panelA,1,3,0,0,1.0,1.0);
    	easyConstraints(gbl,panelB,1,2,1,0,1.0,1.0);
    	easyConstraints(gbl,panelC,1,1,2,0,1.0,1.0);
    	easyConstraints(gbl,panelD,1,2,3,0,1.0,1.0);
    	easyConstraints(gbl,panelE,1,2,2,1,1.0,1.0);
    	easyConstraints(gbl,panelF,1,1,1,2,3.0,2.0);
    	easyConstraints(gbl,panelG,1,1,3,2,1.0,1.0);
    	easyConstraints(gbl,panelH,2,1,0,3,1.0,1.0);
    	easyConstraints(gbl,panelJ,2,1,2,3,1.0,1.0);

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by canersen View Post
    In the book it says weights will scale the cells, i tried to scale F cell (3.0,2.0) since it is comman but it doest work. Can you help me correcting the constraints.

    Java Code:
     
    	easyConstraints(gbl,panelA,1,3,0,0,1.0,1.0);
    	easyConstraints(gbl,panelB,1,2,1,0,1.0,1.0);
    	easyConstraints(gbl,panelC,1,1,2,0,1.0,1.0);
    	easyConstraints(gbl,panelD,1,2,3,0,1.0,1.0);
    	easyConstraints(gbl,panelE,1,2,2,1,1.0,1.0);
    	easyConstraints(gbl,panelF,1,1,1,2,3.0,2.0);
    	easyConstraints(gbl,panelG,1,1,3,2,1.0,1.0);
    	easyConstraints(gbl,panelH,2,1,0,3,1.0,1.0);
    	easyConstraints(gbl,panelJ,2,1,2,3,1.0,1.0);
    Look at those grid lines: panel A should have a WxH == 1x4, panels C and F should both have 3x2 etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    canersen is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    i try the original Width X Height without weights :
    Java Code:
    	easyConstraints(gbl,panelA,1,4,0,0,1.0,1.0);
    	easyConstraints(gbl,panelB,3,2,1,0,1.0,1.0);
    	easyConstraints(gbl,panelC,1,1,4,0,1.0,1.0);
    	easyConstraints(gbl,panelD,1,2,5,0,1.0,1.0);
    	easyConstraints(gbl,panelE,1,3,4,1,1.0,1.0);
    	easyConstraints(gbl,panelF,3,2,1,2,1.0,1.0);
    	easyConstraints(gbl,panelG,1,2,5,2,1.0,1.0);
    	easyConstraints(gbl,panelH,4,1,0,4,1.0,1.0);
    	easyConstraints(gbl,panelJ,2,1,4,4,1.0,1.0);
    and i obtained again wrong result:

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Set those weights to 0.0

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Weights don't 'scale the cells'. Weights determine how extra space will be allocated, after providing components their preferredSize. Read the API.

    Problems such as yours arise out of not understanding how GridBagLayout determines rows and columns. The weights can only be correctly applied when there is at least one component that lies solely in that row (gridheight = 1) or solely in that column (gridwidth = 1).

    One way of achieving this is a complex layout is to add 'dummy' components that are never seen, take up no space, but allow GBL to calculate its rows and columns the way you need. Example (reuses the same GBC, I leave it as an exercise for you to change the code to your style):
    Java Code:
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import javax.swing.*;
    
    public class GridBagAtoJ {
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new GridBagAtoJ().makeUI();
          }
        });
      }
    
      public void makeUI() {
    
        JFrame frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
    
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        for (int i = 1; i <= 6; i++) {
          gbc.gridx = i;
          frame.add(Box.createHorizontalGlue(), gbc);
        }
    
        gbc.gridx = 0;
        gbc.weightx = 0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.VERTICAL;
        for (int i = 1; i <= 4; i++) {
          gbc.gridy = i;
          frame.add(Box.createVerticalGlue(), gbc);
        }
    
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
    
    
        JPanel a = new BorderedPanel("A");
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridheight = 4;
        gbc.gridwidth = 1;
        frame.add(a, gbc);
    
        JPanel b = new BorderedPanel("B");
        gbc.gridx = 2;
        gbc.gridheight = 2;
        gbc.gridwidth = 3;
        frame.add(b, gbc);
    
        JPanel c = new BorderedPanel("C");
        gbc.gridx = 5;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        frame.add(c, gbc);
    
        JPanel d = new BorderedPanel("D");
        gbc.gridx = 6;
        gbc.gridheight = 2;
        frame.add(d, gbc);
    
        JPanel e = new BorderedPanel("E");
        gbc.gridx = 5;
        gbc.gridy = 2;
        gbc.gridheight = 3;
        frame.add(e, gbc);
    
        JPanel f = new BorderedPanel("F");
        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.gridheight = 2;
        gbc.gridwidth = 3;
        frame.add(f, gbc);
    
        JPanel g = new BorderedPanel("G");
        gbc.gridx = 6;
        gbc.gridy = 3;
        gbc.gridheight = 2;
        gbc.gridwidth = 1;
        frame.add(g, gbc);
    
        JPanel h = new BorderedPanel("H");
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.gridheight = 1;
        gbc.gridwidth = 4;
        frame.add(h, gbc);
    
        JPanel j = new BorderedPanel("J");
        gbc.gridx = 5;
        gbc.gridwidth = 2;
        frame.add(j, gbc);
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }
    
    class BorderedPanel extends JPanel {
    
      public BorderedPanel(String title) {
        setBorder(BorderFactory.createTitledBorder("Panel " + title));
      }
    }
    And kudos to you for omitting 'I' as a panel identifier.

    db

  10. #10
    canersen is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    It looks like in the desired layout, i need to analyse and study the code...

    thanks Darryl.Burke and all repliers.

Similar Threads

  1. Problem with GridBagLayout
    By ze snow in forum New To Java
    Replies: 20
    Last Post: 02-28-2010, 06:08 AM
  2. GridBagLayout
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 10:12 PM
  3. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 08:54 PM
  4. Java Swing GridBagLayout Problem
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 06-29-2008, 08:51 PM
  5. Problem with GridBagLayout
    By Daniel in forum SWT / JFace
    Replies: 2
    Last Post: 07-01-2007, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •