EDIT: As in the example (now written by me) what I'm trying to achieve is packing JLabel (+JTextFields, not in the example) into JPanel with FlowLayout and sorting these panels with BoxLayout one under another but limitting it with JScrollPane so I can specify how high the view area is and if those JPanels (packed JLabels) exceed the height the user has to scroll but only vertically.
public class Example2 extends JFrame {
JScrollPane scrollPane = new JScrollPane();
JPanel viewPanel = new JPanel();
public Example2() {
setSize(400,300);
buildGUI();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void buildGUI() {
// SCROLLPANE PLACEMENT
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(scrollPane, GroupLayout.PREFERRED_SIZE, 350, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(scrollPane, GroupLayout.PREFERRED_SIZE, 223, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
// REST
scrollPane.setViewportView(viewPanel);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
viewPanel.setLayout(new BoxLayout(viewPanel, BoxLayout.Y_AXIS));
for(int i=0; i<3; i++) {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEADING));
panel.setBackground(new Color(200,i*100,100*i));
for(int j=0;j<20;j++) {
JLabel label = new JLabel("label "+j);
panel.add(label);
}
viewPanel.add(panel);
}
}
public static void main(String[] args) {
new Example2();
}
}