How to stick a container in BoxLayout

I have a parent JPanel with layout Y_AXIS. The children of this container are also JPanels. The components fit well on the stack and they are all tied, but I want them to stick to the top of the parent JPanel, so all the extra space is at the bottom and there is no extra space between the components. I tried using glue, but I could have done something wrong. I also set AllignmentX and AllignmentY to the left and up respectively for all children.

So, I want the parent panel to have a stack of children, which does not stretch in height, and stick to the top (and left, if possible) of the parent container with all the excess space located below, as such:

EDIT:

protected void initContent(JPanel panel) { id = new JTextField(); typename = new JTextField(); currentstep = new JComboBox(); currentowner = new JComboBox(); organization = new JTextField(); area = new JTextField(); active = new JLabel(); finished = new JLabel(); fields = new JList(); linkedassignments = new JList(); jobs = new JList(); JPanel infopanel = new JPanel(); JPanel listpanel = new JPanel(); infopanel.setLayout(new BoxLayout(infopanel, BoxLayout.Y_AXIS)); infopanel.add(Box.createVerticalGlue()); infopanel.add(makeInfoContainer("Id", id, 50, 100)); infopanel.add(makeInfoContainer("Type", typename, 50, 100)); infopanel.add(makeInfoContainer("Organization", organization, 50, 100)); infopanel.add(makeInfoContainer("Area", area, 50, 100)); infopanel.add(makeInfoContainer("Active", active, 50, 100)); infopanel.add(makeInfoContainer("Finished", finished, 50, 100)); infopanel.add(makeInfoContainer("Step", currentstep, 50, 100)); infopanel.add(makeInfoContainer("Owner", currentowner, 50, 100)); listpanel.setLayout(new GridLayout(0,1,5,5)); listpanel.add(makeJListContainer("Fields", fields, 200, 200)); listpanel.add(makeJListContainer("Assignments", linkedassignments, 200, 200)); listpanel.add(makeJListContainer("Jobs", jobs, 200, 200)); panel.add(infopanel); panel.add(listpanel); } private Container makeInfoContainer(String name, Component comp, int labelwidth, int compwidth){ JPanel cont = new JPanel(); cont.setAlignmentY(Container.TOP_ALIGNMENT); cont.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0)); JLabel lbl = new JLabel(name); lbl.setPreferredSize(new Dimension(labelwidth, 25)); cont.add(lbl); comp.setPreferredSize(new Dimension(compwidth, 25)); cont.add(comp); return cont; } private Container makeJListContainer(String name, JList list, int areawidth, int areaheight){ Box cont = new Box(BoxLayout.Y_AXIS); cont.setPreferredSize(new Dimension(areawidth, areaheight)); JLabel label = new JLabel(name); cont.add(Box.createHorizontalGlue()); cont.add(label); JScrollPane pane = new JScrollPane(); pane.setAlignmentY(Component.TOP_ALIGNMENT); pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); pane.add(jobs); cont.add(pane); return cont; } 

Window resulting from code

+2
source share
2 answers

Assuming the layoutManager of the containing panel (i.e. the one passed to initContent) has a horizontal BoxLayout

  • remove all adhesive materials
  • set the alignment Y as info / listPanel to the beginning
  • implement getMaximum from infoPanel to return its pref

Something like (borders just to visualize in which container there is space, it is often difficult to solve with so much nesting :)

 JComponent panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS)); panel.setBorder(BorderFactory.createLineBorder(Color.GREEN)); initContent(panel); // in initContent JPanel infopanel = new JPanel() { @Override public Dimension getMaximumSize() { return getPreferredSize(); } }; infopanel.setBorder(BorderFactory.createLineBorder(Color.RED)); infopanel.setAlignmentY(0f); infopanel.setLayout(new BoxLayout(infopanel, BoxLayout.Y_AXIS)); JPanel listpanel = new JPanel(); listpanel.setBorder(BorderFactory.createLineBorder(Color.BLUE)); listpanel.setAlignmentY(0f); listpanel.setLayout(new GridLayout(0,1,5,5)); 

While you are on it: delete all setXXSize - the hard encoding size is wrong, some reasons .

+4
source

Try removing the glue at the top of the container and add it at the bottom.

Modified Code:

 protected void initContent(JPanel panel) {  [...] JPanel infopanel = new JPanel(); JPanel listpanel = new JPanel(); infopanel.setLayout(new BoxLayout(infopanel, BoxLayout.Y_AXIS));  infopanel.add (Box.createVerticalGlue ()); infopanel.add(makeInfoContainer("Id", id, 50, 100));  [...] infopanel.add(makeInfoContainer("Owner", currentowner, 50, 100));  infopanel.add (Box.createVerticalGlue ()); listpanel.setLayout(new GridLayout(0,1,5,5));  [...] } 
+1
source

All Articles