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

    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");
       JPanel thePanel = new JPanel();
       GridBagLayout gbl = new GridBagLayout();                                            
       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;
       JButton button2 = new JButton("Button 2");
       gbc.gridx = 1;
       gbc.gridy = 1;  
       gbc.gridwidth = 1;
       gbc.gridheight = 1;
       gbc.fill = GridBagConstraints.BOTH;
       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;

    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:
    gb2c.set(2-5, 4);
    add(button1, gb2c);

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    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
    It references a free layout that is supposedly easier to use (never done it myself). It's called MigLayout
    and there is a link.

    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
    Rep Power

    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, 09:32 PM
  2. Help with GridBagLayout please!
    By iBowlerHat in forum AWT / Swing
    Replies: 2
    Last Post: 07-07-2013, 06:10 PM
  3. [AWT] GridBagLayout Help.
    By Sandia_man in forum AWT / Swing
    Replies: 2
    Last Post: 05-23-2010, 08:54 PM
  4. help with gridbaglayout
    By robertbob in forum AWT / Swing
    Replies: 5
    Last Post: 05-18-2010, 04:14 AM
  5. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 08: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