This is my first time really using graphics with Java.
I have a problem where I’m trying to populate a JScrollPane with an undefined number of panels (The following code is just a test; I know it’s written using bad practices, but it’s a test to see how to do it is all).
In the final program I’m going to have an ArrayList of orders (ArrayList size not predetermined), and in a JScrollPane I’m going to have a JPanel that holds a JPanel (each containing several labels) that will have details of each order.
here’s the current test code (the JScrollPane is simply named scrollPane):
JPanel panel = new JPanel();
GroupLayout experimentLayout = new GroupLayout(panel);
ArrayList<JPanel> panelArray = new ArrayList();
for(int i = 0; i <3; i++){
JPanel panel2 = new JPanel();
JLabel label2 = new JLabel("Hello");
JLabel label3 = new JLabel("Hi");
panel2.add(label2);
panel2.add(label3);
//panel.add(panel2);
panelArray.add(panel2);
}
experimentLayout.setHorizontalGroup(
experimentLayout.createParallelGroup()
.addComponent(panelArray.get(0))
.addComponent(panelArray.get(1))
.addComponent(panelArray.get(2))
);
experimentLayout.setVerticalGroup(
experimentLayout.createSequentialGroup()
//.addGroup(experimentLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(panelArray.get(0))
.addComponent(panelArray.get(1))
.addComponent(panelArray.get(2))
);
scrollPane.setViewportView(panel);
When this runs, the JPanels that contain the labels are displayed horizontally (next to each other) instead of under one another.
Any help on how I could go about displaying them vertically would be a great help – Thank you all 🙂
You might find GridLayout to be useful.