Results 1 to 3 of 3
  1. #1
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default GridBagLayout help

    I'm having problem understanding the concept. I was hoping to get a layout like this:

    GridBagLayout help-gbl1.jpg


    Java Code:
    public class SwingExample  extends JPanel{
       public static void main(String[] args) {
       JFrame theFrame = new JFrame("Grid Bag Example");
       theFrame.setSize(900,600);
       theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
       JPanel thePanel = new JPanel();
       theFrame.add(thePanel);  
       
       GridBagLayout gbl = new GridBagLayout();                                            
       thePanel.setLayout(gbl);
       GridBagConstraints gbc = new GridBagConstraints();
       gbc.insets = new Insets(10, 10, 10, 10);
    
       JButton button1 = new JButton("Button 1");  
       // these should give it initial position in cell 0,0 ?
       gbc.gridx = 0; 
       gbc.gridy = 0;  
       // this should give it size of 1x2 ?
       // why doesn't it do that?
       gbc.gridwidth = 1; 
       gbc.gridheight = 2;  
       gbc.fill = GridBagConstraints.BOTH;
       thePanel.add(button1,gbc);
       
       JButton button2 = new JButton("Button 2");
       gbc.gridx = 1;
       gbc.gridy = 1;  
       gbc.gridwidth = 1;
       gbc.gridheight = 1;
       gbc.fill = GridBagConstraints.BOTH;
       thePanel.add(button2,gbc);
    
       JButton button3 = new JButton("Button 3");  
       // these should give it initial position in cell 2,2 ?
       gbc.gridx = 2;  
       gbc.gridy = 2;
       // this should give it size of 3x1 ?
       // how do I know which direction it grows?
       // does it adds columns 4 and 5, or it goes to the left?
       gbc.gridwidth = 3;
       gbc.gridheight = 1;
       gbc.fill = GridBagConstraints.BOTH;
       thePanel.add(button3,gbc);
    
       theFrame.setVisible(true);   
      }
    }

    BTW, is there any way (or different Layout) I could give the table initial size like 7x5 and then add components to span from cell to cell?
    For example if I want to add something that would span from 2nd to 5th column in 4th row, jut to type something simple like:

    Java Code:
    gb2l.setsize(7,5);
    gb2c.set(2-5, 4);
    add(button1, gb2c);

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: GridBagLayout help

    I haven't used it in a long time. You may want to check the tutorials in my signature below. They show examples.
    Also check out this post https://www.java-forums.org/new-java...t-problem.html
    It references a free layout that is supposedly easier to use (never done it myself). It's called MigLayout
    and there is a link.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: GridBagLayout help

    I've been reading Oracle docs, various tutorials and watching numerous videos, for more than 6 hours, and still can't get it to work :) I'm starting to lose my nerves with it. Seen that Mig, wanted to try it, but it's giving me security problems:

    GridBagLayout help-security.jpg

Similar Threads

  1. GridBagLayout
    By Joshua_Parsons in forum AWT / Swing
    Replies: 1
    Last Post: 10-29-2016, 10:32 PM
  2. Help with GridBagLayout please!
    By iBowlerHat in forum AWT / Swing
    Replies: 2
    Last Post: 07-07-2013, 07:10 PM
  3. [AWT] GridBagLayout Help.
    By Sandia_man in forum AWT / Swing
    Replies: 2
    Last Post: 05-23-2010, 09:54 PM
  4. help with gridbaglayout
    By robertbob in forum AWT / Swing
    Replies: 5
    Last Post: 05-18-2010, 05:14 AM
  5. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 09:35 AM

Posting Permissions

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