I am designing a Pizza Delivery simulator for a class project. My team and I have most of it done, but we are struggling with the GUI.
I need to be able to add items to the order. Each time I click the add item button, it creates a new AddItemPanel (a panel I created that extends JPanel), and adds it to the JScrollPane.
The problem I am running into is it will only add ONE AddItemPanel to the scrollPane. I am not sure if they are being hidden underneath the first one, or if I am just doing something stupid.
Here is the relevant code:
JPanel itemPanel = new JPanel(new GridLayout(0, 1));
JViewPort viewPort = new JViewport();
viewPort.setLayout(new GridLayout(0, 1)); // not sure if I need this line
JScrollPane scrollPane = new JScrollPane(viewPort);
itemPanel.add(scrollPane);
// other stuff
JButton addItemButton = new JButton("Add Item");
addItemButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
viewPort.add(new AddItemPanel());
validate();
}
});
So my questions are: Is this even possible with a JScrollPane? If so, how do I do it? If not, how would I accomplish something similar?
(PS. I have linked screen shots in case they are helpful in explaining.)