Reputation: 113
I have a window, containing a frame and JScrollBar. JScrollBar has panel, whose size i want to change at run time if any child component is added with it. Child component size is constant and should not change.
I have Horizontal scrollBar disabled. So if placement of Child Component exceeds panel width, it should go to next row and panel height should change automatically.
Here is code snippet
JScrollPane scrollPane = new JScrollPane();
menuPane = new JPanel();
menuPane.setLayout(new FlowLayout(FlowLayout.CENTER));
scrollPane.setViewportView(menuPane);
MenuTray mtFile = new MenuTray("File"); // MenuTray extends JPanel
menuPane.add(mtFile);
Menu mNew = new Menu((new ImageIcon(MenuScreen.class.getResource("/com/srinar/res/New.png"))), "New"); // Menu extends JLabel
mtFile.add(mNew);
frame.getContentPane().add(scrollPane);
Upvotes: 0
Views: 290
Reputation: 324088
. So if placement of Child Component exceeds panel width, it should go to next row and panel height should change automatically
The FlowLayout will wrap components automatically but it doesn't recalculate the preferred size with the components on the new row.
Instead you can use the Wrap Layout which extends FlowLayout so it can recalculate the preferred size correctly when components wrap.
Note, the wrapping will occur even if you don't use a scroll pane.
Upvotes: 2