I have a GUI where the left side holds an image and the right side is a panel which holds some label objects. I want it so that if there are more objects on the right panel than can fit, a scroll bar will appear.
The problem I have is that when a lot of objects are added to the right panel, the whole frame resizes.
Here are some screenshots. THe first one is the size I want the window to always be and the second image shows the resizing issue.

Second image:

Here is the code for the panel which holds the image panel and the side bar panel:
/**
* image panel - displays image and editing area
*/
ImagePanel imagePanel = null;
// Holds the labels
private LabelHolder lHolder = new LabelHolder();
/**
* handles New Object button action
*/
public void addNewPolygon() {
imagePanel.addNewPolygon(null);
}
public ContainerPanel(String imageFilename, JFrame frame) {
//setup main window panel
addComponents(imageFilename, frame);
}
public void addComponents(String imageFilename, JFrame frame) {
setLayout(new FlowLayout());
// Polygon Title
JLabel labelPanelTitle = new JLabel("<html><b>Polygons</b></html>");
labelPanelTitle.setBorder(new EmptyBorder(0, 0, 0, 150));
Font f = new Font("LabelPanel", Font.PLAIN, 24);
labelPanelTitle.setFont(f);
labelPanelTitle.setAlignmentX(CENTER_ALIGNMENT);
// Create and set up the image panel.
try {
imagePanel = new ImagePanel(imageFilename, frame, this);
} catch (Exception e1) {
e1.printStackTrace();
}
imagePanel.setOpaque(true); //content panes must be opaque
JPanel objectsPanel = new JPanel();
objectsPanel.setLayout(new BoxLayout(objectsPanel, BoxLayout.Y_AXIS));
JPanel labelHolderContainer = new JPanel();
labelHolderContainer.setLayout(new BoxLayout(labelHolderContainer, BoxLayout.Y_AXIS));
labelHolderContainer.add(lHolder);
JScrollPane scroller = new JScrollPane(labelHolderContainer);
// add the title
objectsPanel.add(labelPanelTitle);
// Add a spacer
objectsPanel.add(Box.createRigidArea(new Dimension(0,25)));
// Add all the labels
objectsPanel.add(scroller);
addNewLabel("Some Object");
addNewLabel("Some Object");
addNewLabel("Some Object");
addNewLabel("Some Object");
addNewLabel("Some Object");
addNewLabel("Some Object");
addNewLabel("Some Object");
addNewLabel("Some Object");
addNewLabel("Some Object");
addNewLabel("Some Object");
// addNewLabel("Some Object");
// addNewLabel("Some Object");
// addNewLabel("Some Object");
// addNewLabel("Some Object");
add(imagePanel);
add(Box.createRigidArea(new Dimension(10,0)));
add(objectsPanel);
}
public void addNewLabel(String labelName) {
lHolder.add(new Label(labelName, imagePanel.getCurrentColour()));
lHolder.add(Box.createRigidArea(new Dimension(10,10)));
// set new colour
imagePanel.setCurrentColour(imagePanel.getRandomColour());
}
have to override max size for parent of
JScrollPane, or overrideJPanel(insideJScrollPane) helt whatever demonstrated on pictures, no idea for better help sooner post an SSCCEin the case that you want to natural scrolling (no issue to put
Iconswith narrative) then touse JList
use JTable with one Column
EDIT
remove
JFrame#pack()from method(s) toadd/modify/removeItems inJScrollPane, otherwise isn’t possible change size ofTop-Level Containerson the Screen