I'm having an issue with a BoxLayout JPanel not properly displaying horizontal glue. I believe that I've narrowed down the issue to there being no extra horizontal space for the glue to pull, as a rigid area creates space between each panel with no problems. With that being said, I can't seem to find what component or setting is causing this.
Here's the method that contains the code in question (I've surrounded the section of code where I add the horizontal glue with a multiline comment):
public void initialize() {
scrollPanel.removeAll();
for (Item item : getController().getCart().getItemList()) {
//Container
itemContainerPanel = new JPanel();
itemContainerPanel.setBorder(new EmptyBorder(0, 10, 10, 0));
itemContainerPanel.setLayout(new BoxLayout(itemContainerPanel, BoxLayout.Y_AXIS));
//Content panel
itemPanel = new JPanel();
itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.X_AXIS));
itemPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
//itemPanel.setBorder(new LineBorder(Color.BLACK));
itemContainerPanel.add(itemPanel);
//Details left
detailsLeftPanel = new JPanel();
detailsLeftPanel.setLayout(new BoxLayout(detailsLeftPanel, BoxLayout.Y_AXIS));
detailsLeftPanel.setAlignmentY(Component.TOP_ALIGNMENT);
titlePanel = new JPanel();
FlowLayout titlePanelLayout = new FlowLayout(FlowLayout.LEFT);
titlePanelLayout.setHgap(0);
titlePanelLayout.setVgap(0);
titlePanel.setLayout(titlePanelLayout);
productNameLabel = new JLabel();
productNameLabel.setAlignmentX(LEFT_ALIGNMENT);
productNameLabel.setAlignmentY(TOP_ALIGNMENT);
titlePanel.add(productNameLabel);
detailsLeftPanel.add(titlePanel);
datesBookedPanel = new JPanel();
FlowLayout datesBookedPanelLayout = new FlowLayout(FlowLayout.LEFT);
datesBookedPanelLayout.setHgap(0);
datesBookedPanelLayout.setVgap(0);
datesBookedPanel.setLayout(datesBookedPanelLayout);
datesBookedLabel = new JLabel();
datesBookedLabel.setAlignmentX(LEFT_ALIGNMENT);
datesBookedPanel.setAlignmentY(TOP_ALIGNMENT);
datesBookedPanel.add(datesBookedLabel);
detailsLeftPanel.add(datesBookedPanel);
//Details right
detailsRightPanel = new JPanel();
detailsRightPanel.setLayout(new BoxLayout(detailsRightPanel, BoxLayout.Y_AXIS));
detailsRightPanel.setAlignmentY(Component.TOP_ALIGNMENT);
removePanel = new JPanel();
removePanel.setLayout(new BoxLayout(removePanel, BoxLayout.X_AXIS));
detailsRightPanel.add(removePanel);
cartItemPriceLabel = new JLabel();
cartItemPriceLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
removePanel.add(cartItemPriceLabel);
removeBtn = new JButton("Remove");
removeBtn.setAlignmentY(Component.CENTER_ALIGNMENT);
removePanel.add(removeBtn);
/* ITEM PANEL BUILD WITH HORIZONTAL GLUE */
itemPanel.add(detailsLeftPanel);
itemPanel.add(Box.createHorizontalGlue());
itemPanel.add(detailsRightPanel);
}
Any help would be appreciated. Thanks and have a nice day.
EDIT:
I've included an image
that shows the output of the code section above. The red lines I've drawn on the image show where the horizontal glue is placed. The glue should push the panel with the price tag and remove button to the right to fill the width of the window, but it doesn't.
The problem is that the "itemPanel" is being displayed at its preferred size so you will never see the "horizontal glue"
You add you "itemPanel" to the "itemContainerPanel". You should not need this second panel. In any case I don't see where you add the "itemContainerPanel" to its parent container.
I would guess you should be adding the "itemPanel" directly to your "scrollPanel". So the "scrollPanel" needs to use a layout that will allow its child components to expand horizontally to fill the entire space. So I would guess the "scrollPanel" should be using the vertical BoxLayout.