Results 1 to 5 of 5
Thread: Help with LayoutManagers
- 03-12-2011, 11:48 AM #1
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:
-
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
-
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);
- 03-12-2011, 12:14 PM #4
-
Similar Threads
-
Swing and LayoutManagers
By gammaman in forum New To JavaReplies: 2Last Post: 07-24-2009, 02:17 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks