Results 1 to 5 of 5
  1. #1
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default Help with LayoutManagers

    Hi, I would like to create a JFrame with the following setup:
    A contentPane with 3 panels added called topPnl, middlePnl and lowerPnl aranged from top to bottom.

    There is an array of smaller panels called slotPanels[] that each hold a single JComboBox called slots[].
    There is a loop that adds a certain number of these panels in a gridLayout to each of the top middle and lower panels.
    All of this works fine but because the contentPane uses a gridLayout the top middle and lower panels are the same size, and because each of these three panels hold a different number of slotPanels, the slotPanels are not evenly spaced. Does anyone have another suggestion on how to get all the slotPanels evenly spaced. the top middle and lower panels do not need to be the same height. Here is my code:

    Java Code:
    public class SelectionFrame extends JFrame implements ActionListener
    {
        private JButton addBtns[] = new JButton[11];
        private JPanel slotPanels[] = new JPanel[11];
        private JComboBox slots[] = new JComboBox[11];
        
        public SelectionFrame()
        {
            createMenu();
            //create panels for empty slots
            JPanel topPnl = new JPanel();
                topPnl.setLayout(new GridLayout(3,2,5,5));
                topPnl.setBorder(new TitledBorder("Top Order"));
            JPanel middlePnl = new JPanel(); 
                middlePnl.setLayout(new GridLayout(2,2,5,5));
                middlePnl.setBorder(new TitledBorder("Middle Order"));
            JPanel lowerPnl = new JPanel(); 
                lowerPnl.setLayout(new GridLayout(6,2,5,5));
                lowerPnl.setBorder(new TitledBorder("Lower Order"));
    
                //add contents to slots and slots into panels
            FlowLayout slotSetup = new FlowLayout(FlowLayout.LEFT,5,3);
            for(int i=0; i<slots.length; i++)
            {
                slotPanels[i] = new JPanel();
                slotPanels[i].setLayout(slotSetup);
                addBtns[i] = new JButton("Add");
                addBtns[i].addActionListener(this);
                slotPanels[i].add(addBtns[i]);
                slots[i] = new JComboBox();
                Font f = new Font("arial", Font.BOLD, 12);
                slots[i].setFont(f);
                slots[i].addItem("Empty                    ");
                slotPanels[i].add(slots[i]);
                if(i<3)
                    topPnl.add(slotPanels[i]);
                else
                    if(i<5)
                        middlePnl.add(slotPanels[i]);
                    else
                        lowerPnl.add(slotPanels[i]);
            }
    
            Container c = getContentPane();
            c.setLayout(new GridLayout(3,1,5,5));
            c.add(topPnl);
            c.add(middlePnl);
            c.add(lowerPnl);
    }
    Last edited by BiteMuncher; 03-12-2011 at 11:58 AM.
    Sorry, I only speak machine language. Yes or a No?:confused:

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

    Default

    Can you post a picture of what you have and another of what you are trying to achieve?

    Also, you'll want to create and post an SSCCE

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

    Default

    Also, if you don't care if top, middle and bottom JPanels are the same size, you could always use a BoxLayout to hold them.

    Java Code:
          Container c = getContentPane();
          //!! c.setLayout(new GridLayout(3, 1, 5, 5));
          c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
          c.add(topPnl);
          c.add(middlePnl);
          c.add(lowerPnl);

  4. #4
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Also, if you don't care if top, middle and bottom JPanels are the same size, you could always use a BoxLayout to hold them.

    Java Code:
          Container c = getContentPane();
          //!! c.setLayout(new GridLayout(3, 1, 5, 5));
          c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
          c.add(topPnl);
          c.add(middlePnl);
          c.add(lowerPnl);
    Thanks!, they are evenly spaced now.
    How does the BoxLayout space the components?
    Sorry, I only speak machine language. Yes or a No?:confused:

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

Similar Threads

  1. Swing and LayoutManagers
    By gammaman in forum New To Java
    Replies: 2
    Last Post: 07-24-2009, 02:17 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
  •