Results 1 to 2 of 2

Thread: GridBagLayout

  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default GridBagLayout

    So I've been getting away from the default layout manager and have been playing with GridBagLayout. I actually like this layout a lot, its a rather simple way to give a complex layout.

    My trouble is that I have, say 100 buttons, I'm adding to a JPanel. This JPanel is inside a JScrollPane (So when there are too many buttons, you can scroll to see the rest). I'm trying to use the GridBagLayout manager to add each button to the NORTH. So I set a constraint;

    Java Code:
    c.anchor = GridBagConstraints.NORTH;
    As I understand this, if the Component's Displaying Area (the JPanel) has extra room, this constraint will distribute the extra space based around the constraint. I choose to set mine Centered at the top, which as I understand to be NORTH.

    When I run this app, the button continues to display in the center.

    Java Code:
    JPanel buttonHolder = new JPanel();
    JScrollPane sp = new JScrollPane(buttonHolder);
    GridBagConstraints c = new GridBagConstraints();
    Dimension d = new Dimension(160,560);
    c.gridx = 100;
    c.anchor = GridBagConstraints.NORTH;
    
    sp.setPreferredSize(d);
    mainPanel.add(sp);
    
    for(int i=0; i<counter; i++){
    	buttonBuddies[i].setText(buddies[i]);
            buttonHolder.add(buttonBuddies[i],c);
    }
    The above code is not exact, its the pieces I think are important in solving the puzzle. Its the setup, the JPanel and JScrollPane (w/Dimension), and the buttons being added using the constraints.

    Thanks for any suggestions

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    its a rather simple way to give a complex layout.
    A simpler way is to use nested panels with each panel using the appropriate layout manger. You are not forced to use a single layout manager.

    The above code is not exact, its the pieces I think are important in solving the puzzle
    When you have a problem you don't know what is and what isn't important. That is why you post a SSCCE to demonstrate the problem.

Similar Threads

  1. GridBagLayout
    By carderne in forum New To Java
    Replies: 8
    Last Post: 01-25-2009, 02:06 PM
  2. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 08:54 PM
  3. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 08:35 AM
  4. GridBagLayout...please help
    By newtojava7 in forum Advanced Java
    Replies: 1
    Last Post: 02-17-2008, 01:16 AM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 08:03 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
  •