Reputation: 841
I'm trying to add precreated JPanel instances into a bigger JPanel into a JScrollPane, but I can't get it to work.
I'm using NetBeans to create the container, then using the context menu to include it into a scroll panel.
To add the JPanel at runtime I'm doing this:
ActivityPanel actPanel;
for(Reservation r: mainFrame.getKiosko().getReservations()) {
if( r.getUser().equals(user)) {
actPanel = new ActivityPanel(r.getActivity(),mainFrame);
actPanel.setHour(r.getHour());
Dimension actDim = new Dimension(600, 100);
actPanel.setPreferredSize(actDim);
actPanel.setMaximumSize(actDim);
actPanel.setMinimumSize(actDim);
pnlReservations.add(actPanel);
}
}
But it just is not working. Should I create the ScrollPanel at runtime? If so How should I do it?
pnlReservation
has a BoxLayout by page axis.
Upvotes: 0
Views: 265
Reputation: 324108
actPanel.setPreferredSize(actDim);
Don't try to manually control the preferred size of a component. The size of the panel should be determined by the components you add to the panel.
Same with your pnlReservation
, don't hardcode a preferred size. The layout manager will determine its preferred size based on the preferred size of the components added to it.
Then the scrollbars will appear automatically as the preferred size of the pnlReservation
dynamically changes when you add components to it.
Upvotes: 1