Results 1 to 8 of 8
  1. #1
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Unhappy Gridbaglayout not working as expected

    Hi there,
    I'm trying to layout three panels vertically in a JScrollPane.
    I've made a class which extends JPanel and implements Scrollable. I have placed it into the JScrollPane. In the panel I have placed TopPanel, MiddlePanel and BottomPanel.
    However, the panels are appearing horizontally in the scrollpane, instead of vertically.

    If I set the preferred size of the panel, the panels are placed vertically, however the third panel is not shown and the bottom of the second panel is missing.

    How do I make all three panels appear vertically, without cutting out any panels?

    Thanks for your attention,
    Berkeleybross

    Java Code:
    public class AdviceNotePanel extends JPanel implements Scrollable{
        public AdviceNotePanel () {
            GridBagConstraints myConstraints;       
            setBackground(Color.WHITE);
           //setPreferredSize(new Dimension(595, 842));
    
            JPanel topPanel = new JPanel (new GridBagLayout());
            topPanel.setBackground(Color.WHITE);
            myConstraints = new GridBagConstraints();
            myConstraints.gridx = 1;
            myConstraints.gridy = 1;
            myConstraints.gridwidth = 1;
            myConstraints.gridheight = 1;
            myConstraints.weightx = 1;
            myConstraints.weighty = 1;
            myConstraints.anchor = GridBagConstraints.CENTER;
            myConstraints.fill = GridBagConstraints.BOTH;
            myConstraints.insets = new Insets(0, 0, 0, 0);
            myConstraints.ipadx = 0;
            myConstraints.ipady = 0;
    
            add(topPanel, myConstraints);
            //Code adding components to top panel
            
            JPanel middlePanel = new JPanel(new GridBagLayout());
            myConstraints = new GridBagConstraints();
            myConstraints.gridx = 1;
            myConstraints.gridy = 2;
            myConstraints.gridwidth = 1;
            myConstraints.gridheight = 1;
            myConstraints.weightx = 1;
            myConstraints.weighty = 1;
            myConstraints.anchor = GridBagConstraints.BASELINE;
            myConstraints.fill = GridBagConstraints.BOTH;
            myConstraints.insets = new Insets(0, 0, 0, 0);
            myConstraints.ipadx = 0;
            myConstraints.ipady = 0;
            add(middlePanel, myConstraints);
            // Code adding components to middlepanel
    
            JPanel bottomPanel = new JPanel(new GridBagLayout());
            bottomPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
            bottomPanel.setBackground(Color.WHITE);
    
            myConstraints = new GridBagConstraints();
            myConstraints.gridx = 1;
            myConstraints.gridy = 3;
            myConstraints.gridwidth = 1;
            myConstraints.gridheight = 1;
            myConstraints.weightx = 1;
            myConstraints.weighty = 1;
            myConstraints.anchor = GridBagConstraints.CENTER;
            myConstraints.fill = GridBagConstraints.BOTH;
            myConstraints.insets = new Insets(0, 0, 0, 0);
            myConstraints.ipadx = 0;
            myConstraints.ipady = 0;
    
            add(bottomPanel, myConstraints);
            // Code adding components to bottomPanel
    
        }
    
        // Code implementing scrollable interface
    }

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

    Default

    If you can, can you post a small compilable runnable example of your problem? Just at a glance though, you know that gridx and gridy both are 0 based, not 1 based.

    Also, based on your description, it may not be important that the top, middle and bottom panel implement GridBagLayout, however it is critical that AdviceNotePanel, the "this" JPanel, implement GridBagLayout, and I don't see that you've done this, at least not in the code that you've posted.

    Also, if all you want are three equally sized JPanels stacked on top of each other, why not use the far simpler GridLayout(0, 1)?
    Last edited by Fubarable; 07-05-2010 at 09:39 PM.

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Hi Fubarable,
    I'll change the constraints to 0 based. If that doesnt work, then I'll try to make a small program and post it here.

    The AdviceNotePanel does indeed implement gridbaglayout, i do it when i instanciate the AdviceNotePanel so you dont see it in this code :P

    The JPanels should be the same width, but not the same height. They have quite a few components in them already, I left them out so to make clearer code.

    Ill get back to you if 0basing them has any effect :)

    Berkeleybross

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Heh, its amazing what SSCCE's can do.
    Turns out I wasn't using GridBagLayout for the panel.
    I had been setting the layout as using GridBagLayout after putting all the components in the panel, so it didn't work.

    I seem to have a habit of looking for hours at my problem, giving in and coming here only for the solution to be staring me in the face all along.

    Thanks for your time though, much appreciated.

    Berkeleybross

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

    Default

    Quote Originally Posted by berkeleybross View Post
    Heh, its amazing what SSCCE's can do.
    Turns out I wasn't using GridBagLayout for the panel.
    I had been setting the layout as using GridBagLayout after putting all the components in the panel, so it didn't work.
    I don't think that this matters as long as it is set to GridBagLayout before pack() or revalidate() is called. For my money though, if you know that the JPanel is going to always need to be using GridBagLayout, then set it near the beginning of its constructor and be done with it.

    Glad you have it fixed. You may wish to mark this thread "solved".

    Oh, another way to stack different height JPanels on top of each other is to use BoxLayout which is easier to use than GridBagLayout.

  6. #6
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    I didnt think it mattered either, apparently it does though :P

    Ill mark it solved now.

    Thanks again,
    Berkeleybross

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

    Default

    Yes, it matters. To see why, read the source of GridBagLayout#addLayoutComponent(Component comp, Object constraints) which calls setConstraints(Component comp, GridBagConstraints constraints) which in turn adds the Component and a clone of the GBC to a Hashtable.

    The values in the Hashtable are read when layoutContainer calls arrangeGrid which calls ArrangeGrid [aside: one has to wonder who was responsible for that particular cr@p in violation of naming conventions] which calls lookupConstraints which provides the value from the Hashtable.

    If GBL is set after adding the components, the Hashtable won't have any entries and the components will be laid out as if added with a null constraint.

    db

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

Similar Threads

  1. help with gridbaglayout
    By robertbob in forum AWT / Swing
    Replies: 5
    Last Post: 05-18-2010, 04:14 AM
  2. GridBagLayout
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 10:12 PM
  3. Replies: 8
    Last Post: 05-28-2008, 07:00 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
  •