Juhan Voolaid ,
Here is the trick for your problem which I faced many many times.
step1:Have your contentPanel as a mainPanel to which added JScrollPane
JPanel contentPanel = new JPanel();
step2:Have seperate panel which holds your others components.
JPanel componentsPanel = new JPanel();
JLabel nameLabel = new JLabel("Name");
JTextField nameTextField = new JTextField(10);
componentsPanel.add(nameLabel);
componentsPanel.add(nameTextField );
step3:add that seperate panel to JScrollPane using setViewportView(JComponent jc) method of the JScrollPane.
JScrollPane contentScrollPane = new JScrollPane();
contentScrollPane.setViewportView(componentsPanel);
contentPanel.add(contentScrollPane);
JFrame frame = new JFrame();
frame.add(contentPanel);
frame.setSize(300,300);
frame.setVisible(true);
Each time you create new components mantain all that components in JPanel and again call the setViewportView(JComponent) method.
supoose I create new Panel componentsPanel2 during runtime add to contentPanel:
code below:
JPanel componentsPanel2 = new JPanel();
JLabel passwordLabel = new JLabel("Password");
JTextField passwordTextField = new JTextField(10);
componentsPanel2.add(passwordLabel);
componentsPanel2.add(passwordTextField);
contentScrollPane.setViewportView(componentsPanel2);
This make your components visible without any resize of your JFrame.
Try with this if you don't get it I will post to you the entire sample code of small demo.
Please let me know.
regards,
Dhilshuk Reddy.